Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 逐个选择每个子表中的元素_Javascript_Jquery - Fatal编程技术网

Javascript 逐个选择每个子表中的元素

Javascript 逐个选择每个子表中的元素,javascript,jquery,Javascript,Jquery,我有下面的html,它基本上是一个父表,里面有一些子表 <table id="roottable" class="tablemain" cellspacing="0" cellpadding="0"> <tbody> <tr> <td></td> <td> <table class="intable" align="

我有下面的html,它基本上是一个父表,里面有一些子表

<table id="roottable" class="tablemain" cellspacing="0" cellpadding="0">
    <tbody>
        <tr>
            <td></td>
            <td>
                <table class="intable" align="center" border="0">
                    <tbody>
                        <tr>
                            <td class="chn" colspan="2" align="center">
                                <div>
                                    <div class="mparent">
                                        <div class="checkbox">
                                            <input type="checkbox" id="ch243" name="ch243" value="243">
                                            <label for="ch243"></label>
                                        </div>
                                        <div class="chtext">Category</div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>Param two</td>
                                <td>
                                    <div class="checkbox">
                                        <input type="checkbox" id="ch244" name="ch244" value="244">
                                        <label for="ch244"></label>
                                    </div>
                                </td>
                        </tr>
                    </tbody>
                </table>
                ......
                ......
                <table class="intable" align="center" border="0">
                ......
                ......    
这是可行的,但是表是从db自动生成的,id事先不知道,而且,表的数量可能会有所不同,所以我没有其他选择,只能选择父表,然后逐个查找其中的每个子表。。。我试过这个

$('table').each(function(){
 $(this).find('input[type="checkbox"]').each(function () {
    // DO STUFFS WITH CHECKBOXES

});
但它不起作用。。。我该怎么办?谢谢。

只需使用:

var tableInputs = {};
$('#roottable input[type="checkbox"]').each(function () {
    var id = $(this).closest('table').attr('id');
    var name = $(this).attr('name');
    tableInputs[id] = {};
    tableInputs[id][name] = $(this).val();
});
这将选中作为表的子项的所有复选框

如果需要分组,请编辑,只需找到最近父表的id,并将其用作对象的索引。您最终得到一个大对象,所有id都作为属性,而每个循环只使用一个。

只需使用:

var tableInputs = {};
$('#roottable input[type="checkbox"]').each(function () {
    var id = $(this).closest('table').attr('id');
    var name = $(this).attr('name');
    tableInputs[id] = {};
    tableInputs[id][name] = $(this).val();
});
$('table tr td').each(function(){  // this line is for main outer table
  $(this).children('table').each(function () {  //this will iterate all the child table
    $(this).find('input:checkbox').each(function(){  //this will find checkbox inside each table
       //Your Stuff
    });
  });
});
这将选中作为表的子项的所有复选框

如果需要分组,请编辑,只需找到最近父表的id,并将其用作对象的索引。最终会得到一个大对象,所有id都作为属性,而每个循环只使用一个

$('table tr td').each(function(){  // this line is for main outer table
  $(this).children('table').each(function () {  //this will iterate all the child table
    $(this).find('input:checkbox').each(function(){  //this will find checkbox inside each table
       //Your Stuff
    });
  });
});
注意:-我在这里没有使用id选择器,因为提问者提到他事先不知道id

注意:-我在这里没有使用id选择器,因为提问者提到他事先不知道id


最后的代码块看起来可以接受,只是没有阻止表选择器也选择外部表。如前所述,这将导致每组复选框被考虑两次——一次作为其自身表的一部分,另一次实际上首先作为外部表的后代

请尝试更具体的选择器:

$('#roottable .intable').each(...)

最后的代码块看起来可以接受,只是没有阻止表选择器也选择外部表。如前所述,这将导致每组复选框被考虑两次——一次作为其自身表的一部分,另一次实际上首先作为外部表的后代

请尝试更具体的选择器:

$('#roottable .intable').each(...)

我会这样做:

$('#roottable').find('.intable').each(function(index, elt) {
  // operate now on each "child table"
  var $childTable = $(elt);
  $childTable.find('input[type="checkbox"]').each(function(i, cb){
    // Do your stuff with the checkoxes of the current ".intable" table
  });
});

我会这样做:

$('#roottable').find('.intable').each(function(index, elt) {
  // operate now on each "child table"
  var $childTable = $(elt);
  $childTable.find('input[type="checkbox"]').each(function(i, cb){
    // Do your stuff with the checkoxes of the current ".intable" table
  });
});


请为您的回答提供一些解释。这不会提供每个表的复选框分组。谢谢编辑,我认为使用roottable会更好,而且faster@Alnitak这取决于表的每个分组的代码是否需要不同OP说他需要移动到下一个嵌套表-这意味着分组。请为您的回答提供一些解释这不会提供复选框的每个表分组。谢谢编辑,我认为使用roottable会更好,而且faster@Alnitak这取决于每个表分组的代码是否需要不同,OP说他需要移动到下一个嵌套表-这意味着分组。请为您的操作提供一些解释answer@Huangism..plz看小提琴,小提琴很好,但这并不能真正解释代码是如何工作的。这将有助于OP和任何人在未来看这篇文章请提供一些解释,为您的建议answer@Huangism..plz看fiddle。fiddle很好,但这并不能真正解释代码是如何工作的。这将有助于OP和以后阅读此文章的任何人后者看起来是正确的,除了无法将根表与内表分开之外。请定义不适用于每一组表的代码,它们是相同的还是不同的?它们是不同的。不过我让它工作了。使用Kartikeya的解决方案,然后使用一些条件语句。谢谢。后者看起来是正确的,只是没有将根表与intable表分开。请定义不适用于每一组表的代码,它们是相同的还是不同的?它们是不同的。不过我让它工作了。使用Kartikeya的解决方案,然后使用一些条件语句。谢谢