gpt4 book ai didi

Jquery如何计算选中和禁用复选框的数量

转载 作者:行者123 更新时间:2023-12-03 22:31:49 29 4
gpt4 key购买 nike

当有 5 个选中的复选框时,我尝试禁用所有未选中的复选框。

我的代码在这里不起作用,它是:http://jsfiddle.net/mtYtW/18/

我的 Jquery:

var countchecked = $('table input[type="checkbox"]').find(":checked").length

if(countcheckhed > 5) {
$("table input:checkbox").attr("disabled", true);
} else {}

我的 HTML:

<table cellspacing="0" cellpadding="0" width="770px;">
<tbody><tr style="border: 1px solid green; height: 40px; font-size: 14px;">
<th>Feature 1</th>
<th>Feature 2</th>
<th>Feuture 3</th>
</tr>

<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
<tr>
<td class="checkit"><input type="hidden" value="0" name="search[windows_is_true]"><input type="checkbox" value="1" name="search[windows_is_true]" id="search_windows_is_true"></td>
<td>Test 1</td>
<td>Test 2</td>
<td>Test 3</td>
<td>Test 4</td>
<td>Test 5</td>
<td>Test 6</td>
</tr>
</tbody></table>

最佳答案

以下内容应该可以满足您的需求:

$("table input[type=checkbox]").click(function(){
var countchecked = $("table input[type=checkbox]:checked").length;

if(countchecked >= 5)
{
$('table input[type=checkbox]').not(':checked').attr("disabled",true);
}
else
{
$('table input[type=checkbox]').not(':checked').attr("disabled",false);
}

});

Example for your needs

(通用)以下命令将禁用所有未选中的复选框:

$('input[type=checkbox]').not(':checked').attr("disabled","disabled");

Generic Disable Example

关于Jquery如何计算选中和禁用复选框的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6960921/

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