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 如何使用JQuery设置基于第二列td匹配的复选框?_Javascript_Jquery_Html Table - Fatal编程技术网

Javascript 如何使用JQuery设置基于第二列td匹配的复选框?

Javascript 如何使用JQuery设置基于第二列td匹配的复选框?,javascript,jquery,html-table,Javascript,Jquery,Html Table,我想设置第二列记录匹配时复选框是否选中。当记录与第二列匹配时,必须设置同一行的复选框是否选中 $("#ContentPlaceHolder1_GridView1 tr td:nth-child(2)").each(function () { //alert($(this).text()); for(var i = 0;i<data.d.length;i++){ if ($(this).text() === da

我想设置第二列记录匹配时复选框是否选中。当记录与第二列匹配时,必须设置同一行的复选框是否选中

$("#ContentPlaceHolder1_GridView1 tr td:nth-child(2)").each(function () {
                            //alert($(this).text());
    for(var i = 0;i<data.d.length;i++){
        if ($(this).text() === data.d[i]) {    // "222" === "222"
            alert('data =' + data.d[i] + '  $(this).text() = ' + $(this).text());

            //here I have to set checkbox
        }
    }
});
以下是我的html代码:

<table id="ContentPlaceHolder1_GridView1">
<tbody>
    <tr>
        <th> </th>                //No column header for checkbox
        <th>Documnet NO</th>
    </tr>
    <tr>
        <td><input id="Checkbox1" class="checkBoxClass" type="checkbox"></td>
        <td>111</td>
    </tr>
    <tr>
        <td><input id="Checkbox2" class="checkBoxClass" type="checkbox"></td>
        <td>222</td>
    </tr>
    <tr>
        <td><input id="Checkbox3" class="checkBoxClass" type="checkbox"></td>
        <td>333</td>
    </tr>
</body>
到目前为止,警报的匹配记录仍在运行

$这指的是td元素的上下文。你可以

1查找同级td元素

2在td中查找输入元素

3将复选框选中状态设置为true

$("#ContentPlaceHolder1_GridView1 tr td:nth-child(2)").each(function () {
 for(var i = 0;i<data.d.length;i++){
    if ($(this).text() === data.d[i]) {     
       $(this).siblings().find(':checkbox').prop('checked' , true);
    }
});

注意:您还有输入元素的重复ID。ID必须是唯一的。不能使用ID选择器以使用相同ID的多个元素为目标。您可以使用公共类和类选择器来将它们全部设置为目标。

您可以使用DOM遍历方法来设置:checkbox,然后使用.propkey,value来设置其属性

$("#ContentPlaceHolder1_GridView1 tr td:nth-child(2)").each(function () {
    for (var i = 0; i < data.d.length; i++) {
        if ($(this).text() === data.d[i]) {
            $(this).closest('tr') //target common parent i.e. TR
            .find(':checkbox') //Target Checkbox
            .prop('checked', true);
        }
    }
});

为什么使用复选框4有多个ID?