0

I need to check/uncheck a checkbox that exist inside a row under table body when it is clicked anywhere within a row except a link. I tried to make it working by using jQuery's prop, but something is not working properly.

I have a table structure like below:

<table id="anywhere_click">
<thead>
    <tr>
        <th><input type="checkbox" onclick="selectAll('myDataID[]');" /></th>
        <th>Title</th>
        <th>Details</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><input type="checkbox" name="myDataID[]" value="1" /></td>
        <td>Stackoverflow</td>
        <td>Some text here....</td>
    </tr>
    <tr>
        <td><input type="checkbox" name="myDataID[]" value="2" /></td>
        <td>Google</td>
        <td>
            <a href="aaa.html">This is a Link</a><br />
            Some text here....<br />
            <img src="something.jpg" />
        </td>
    </tr>
    <tr>
        <td><input type="checkbox" name="myDataID[]" value="3" /></td>
        <td>test</td>
        <td>Some text here....</td>
    </tr>
</tbody>
</table>
4

2 回答 2

6
$('#anywhere_click tbody tr').click(function (e) {
    if(!$(e.target).is('#anywhere_click td input:checkbox'))
    $(this).find('input:checkbox').trigger('click');
});

$('#anywhere_click tr a').click(function (e) {
    e.stopPropagation();
});
于 2013-05-10T09:38:14.513 回答
0

如果您在网站加载时检查过。

您必须添加已<td><input type="checkbox" name="myDataID[]" value="3" /></td> 选中的标签。

<td><input type="checkbox" name="myDataID[]" value="3" checked /></td>

使用您要选中和未选中的按钮。

信息:信息

示例:选中和未选中按钮的示例

于 2013-05-10T09:36:09.593 回答