Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 表标记内的jquery复选框无法访问_Javascript_Jquery_Html_Checkbox - Fatal编程技术网

Javascript 表标记内的jquery复选框无法访问

Javascript 表标记内的jquery复选框无法访问,javascript,jquery,html,checkbox,Javascript,Jquery,Html,Checkbox,我正在尝试访问表标记内的所有复选框。。。。 最初,所有复选框都在div标记内,当时代码运行良好,我可以访问每个复选框 $('#div_id').children('input').each(function() { if(this.type == 'checkbox') { if($(this).attr('id') == 1) { this.checked = true; } } }); 现

我正在尝试访问表标记内的所有复选框。。。。 最初,所有复选框都在div标记内,当时代码运行良好,我可以访问每个复选框

   $('#div_id').children('input').each(function()
{
    if(this.type == 'checkbox')
    {
        if($(this).attr('id') == 1)
        {
            this.checked = true;
        }
    }
});
现在,当我将复选框放在表中时,它将停止工作。我尝试将“div\u id”更改为“table\u id”,但仍然没有成功。有什么建议吗


所有复选框和表都是动态创建的。
。子项仅沿树向下移动单个元素(此处描述)

您现在需要的是
.find
,它遍历整个子元素树,因此您的代码变成:

$('div_id').find('input').each(function()
{
    if(this.type == 'checkbox')
    {
        if($(this).attr('id') == 1)
        {
            this.checked = true;
        }
    }
});

.children
仅沿树向下移动单个元素(此处描述)

您现在需要的是
.find
,它遍历整个子元素树,因此您的代码变成:

$('div_id').find('input').each(function()
{
    if(this.type == 'checkbox')
    {
        if($(this).attr('id') == 1)
        {
            this.checked = true;
        }
    }
});

您可以使用选择器api:

$('table#id input[type=checkbox]')


将返回包含所有复选框元素的数组。

您可以使用选择器api:

$('table#id input[type=checkbox]')

将返回一个包含所有复选框元素的数组。

-----------------------------下面的HTML代码--------------------------

<table>
    <tr>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
</table>   

<input type="button" onclick="callFunction()" value="Click" />
<script type="text/javascript">
    function callFunction() {

        $('table input[type=checkbox]').each(function () {
            alert("hi table") //This will be call 4 times.
            if (this.checked) {
                alert("hi inner") ////This will be call 2 times, only for checked checkboxes.
                //this.checked = true;
            }
        });
    }
</script>

--------------------------下面是JS代码--------------------------------

<table>
    <tr>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
</table>   

<input type="button" onclick="callFunction()" value="Click" />
<script type="text/javascript">
    function callFunction() {

        $('table input[type=checkbox]').each(function () {
            alert("hi table") //This will be call 4 times.
            if (this.checked) {
                alert("hi inner") ////This will be call 2 times, only for checked checkboxes.
                //this.checked = true;
            }
        });
    }
</script>

函数callFunction(){
$('table input[type=checkbox]')。每个(函数(){
警报(“hi table”)//这将被调用4次。
如果(选中此项){
警报(“hi-inner”)///这将被调用2次,仅针对选中的复选框。
//this.checked=true;
}
});
}
-----------------------------下面是HTML代码--------------------------

<table>
    <tr>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
</table>   

<input type="button" onclick="callFunction()" value="Click" />
<script type="text/javascript">
    function callFunction() {

        $('table input[type=checkbox]').each(function () {
            alert("hi table") //This will be call 4 times.
            if (this.checked) {
                alert("hi inner") ////This will be call 2 times, only for checked checkboxes.
                //this.checked = true;
            }
        });
    }
</script>

--------------------------下面是JS代码--------------------------------

<table>
    <tr>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" checked="checked" /></td>
        <td><input type="checkbox" /></td>
        <td><input type="checkbox" /></td>
    </tr>
</table>   

<input type="button" onclick="callFunction()" value="Click" />
<script type="text/javascript">
    function callFunction() {

        $('table input[type=checkbox]').each(function () {
            alert("hi table") //This will be call 4 times.
            if (this.checked) {
                alert("hi inner") ////This will be call 2 times, only for checked checkboxes.
                //this.checked = true;
            }
        });
    }
</script>

函数callFunction(){
$('table input[type=checkbox]')。每个(函数(){
警报(“hi table”)//这将被调用4次。
如果(选中此项){
警报(“hi-inner”)///这将被调用2次,仅针对选中的复选框。
//this.checked=true;
}
});
}

将所有代码放在文档中。准备好了吗?您可以简单地使用
$('.'div\u id:checkbox[id=1]')。prop('checked',true)
如果id发生了变化,并且不像code@satpal中那样是静态的,那么使用
$('.'div\u id:checkbox[id='+yourId+'])。prop('checked',true)
。一行代码就足够了。
。每个(
为什么要测试id等于1的属性?既然id应该是唯一的,这对我来说毫无意义。。将所有代码放在文档中。准备好了吗?你可以简单地使用
$('#div\u id:checkbox[id=1]')。prop('checked',true)
如果id发生了变化,而不是像code@satpal中那样是静态的,那么使用
$(''div_id:checkbox[id='+yourId+'])。prop('checked',true)
。一行代码就足够了。
为什么要测试id等于1的属性?既然id应该是唯一的,这对我来说毫无意义。那家伙以前说他的代码工作得很好,所以我猜div_id只是一个占位符、标签或否。没问题,.find救了我几次!那家伙以前说他的代码工作得很好,所以我猜div_id只是一个placeholder、hashtag或no.no probs、.find已经保存了我好几次!您好,您将在代码中看到它将为总共4个表项显示警报(“Hi table”),但您也将看到它发出警报(“Hi inner”)仅显示2次,因为它仅对选中的复选框显示警报。您好,您将在代码中看到,它将对总共4个表项显示警报(“Hi table”),但您也将看到它仅显示2次警报(“Hi inner”),因为它仅对选中的复选框显示警报。