我正在尝试仅检查具有特定值的复选框。 我知道如何获得价值,我知道如何检查所有复选框,但我无法弄清楚如何只检查那些,让我们说值'200'
我的示例代码:
<tr>
<td><input class="chk_box" type="checkbox" value="200" /></td>
<td class="code">200</td>
</tr>
<tr>
<td><input class="chk_box" type="checkbox" value="0" /></td>
<td class="code">0</td>
</tr>
<tr>
<td><input class="chk_box" type="checkbox" value="0" /></td>
<td class="code">0</td>
</tr>
<tr>
<td><input class="chk_box" type="checkbox" value="200" /></td>
<td class="code">200</td>
</tr>
<tr>
<td><input class="chk_box" type="checkbox" value="300" /></td>
<td class="code">300</td>
</tr>
<input type="checkbox" class="checkbox_200" label="check 200" />check 200
所以现在我需要jQuery代码来完成我的代码。 我感谢任何帮助。
顺便说一句。有没有办法一次性检查'200'和'300'?
已更新!
答案 0 :(得分:5)
您的复选框没有value
属性,如果您想根据TD元素的文本内容选中复选框,可以使用filter
方法。
$('td.code').filter(function(){
var txt = this.textContent || this.innerText;
return txt === '200' || txt === '300';
}).prev().find('input').prop('checked', true);
<强>更新强>:
$('tr input[type=checkbox][value=200]').prop('checked', true);
替代:
$('tr input[type=checkbox]').filter(function(){
return this.value === '200' || this.value === '300';
}).prop('checked', true);
答案 1 :(得分:4)
您的复选框没有价值。它下面只有文字。 尝试更改代码并设置输入值。
<input class="chk_box" type="checkbox" value="200" /> 200
<br />
<input class="chk_box" type="checkbox" value="0" />0
<br />
<input class="chk_box" type="checkbox" value="0" />0
<br />
<input class="chk_box" type="checkbox" value="200" /> 200
<br />
<input class="chk_box" type="checkbox" value="300" /> 300
您可以使用此选择器:
input[type=checkbox][value="200"]
对于jquery,请在$(“”)中使用它。
答案 2 :(得分:0)
好的,我最终得到了这个解决方案,稍加修改并感谢@undefined:
var chk = $(this).attr('checked')?true:false;
$('tr input[type=checkbox][value=200]').attr('checked',chk);
这允许我选中/取消选中我感兴趣的值的复选框。
多个值相同:
var chk = $(this).attr('checked')?true:false;
$('tr input[type=checkbox]').filter(function(){
return this.value === '200' || this.value === '300';
}).attr('checked',chk);