gpt4 book ai didi

jquery - 检查表 td 中是否选中了复选框

转载 作者:行者123 更新时间:2023-11-28 04:05:24 29 4
gpt4 key购买 nike

我正在尝试构建一个脚本,使用 jQuery $each 函数检查复选框是否被选中。下面是 HTML 代码:

 <tr>
<td class="categories">World</td>
<td class="category_enabled" style="float: right;">
<input type="checkbox"/>
</td>
</tr>

<tr>
<td class="categories">Economy</td>
<td class="category_enabled" style="float: right;">
<input type="checkbox"/>
</td>
</tr>

<tr>
<td class="categories">Ψυχαγωγία</td>
<td class="category_enabled" style="float: right;">
<input type="checkbox"/>
</td>
</tr>

我相信它类似于(在 .each() 内部):

if ($("find_specific_checkbox").is(":checked")){
console.log("Checked!");
}

关于我如何做到这一点的任何想法。 我正在尝试几个小时,但还没有。

最佳答案

根据您的问题和标记,我相信您打算遍历每个表行元素,并获取每行中复选框的状态。

如果是这种情况,我已经创建了 (1) 代码示例和 (2) 下面的概念验证示例,它们可以执行您想要执行的操作。

代码示例

$('table tr').each(function(i) {
// Cache checkbox selector
var $chkbox = $(this).find('input[type="checkbox"]');

// Only check rows that contain a checkbox
if($chkbox.length) {
var status = $chkbox.prop('checked');
console.log('Table row '+i+' contains a checkbox with a checked status of: '+status);
}
});

工作示例

我已将第一个复选框设置为 checked 状态,因此您可以看到逻辑是如何工作的。

$(function() {
// General/modular function for status logging
var checkboxChecker = function() {
$('table tr').each(function(i) {
// Only check rows that contain a checkbox
var $chkbox = $(this).find('input[type="checkbox"]');
if ($chkbox.length) {
var status = $chkbox.prop('checked');
console.log('Table row ' + i + ' contains a checkbox with a checked status of: ' + status);
}
});
};

// Check checkboxes status on DOMready
checkboxChecker();

// Check again when checkboxes states are changed
$('table tr input[type="checkbox"]').on('change', function() {
checkboxChecker();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td class="categories">World</td>
<td class="category_enabled" style="float: right;">
<input type="checkbox" checked />
</td>
</tr>

<tr>
<td class="categories">Economy</td>
<td class="category_enabled" style="float: right;">
<input type="checkbox" />
</td>
</tr>

<tr>
<td class="categories">Ψυχαγωγία</td>
<td class="category_enabled" style="float: right;">
<input type="checkbox" />
</td>
</tr>
</table>

关于jquery - 检查表 td 中是否选中了复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668804/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com