[英]How to auto checked of checkbox from a value in jquery?
I have a some checkbox with each value .I want to auto checked of checkbox . 我每个值都有一个复选框。我想自动检查checkbox。 I use logic & in this case , but it's not working. 在这种情况下,我使用逻辑&,但是它不起作用。 Example : value sum = 5 : food and water is checked , sum = 4 : water is checked, sum = 3 : candy and food is checked , .... 示例:值sum = 5:检查食物和水,sum = 4:检查水,sum = 3:检查糖果和食物,...。
var sum = 5; var list = document.getElementsByClassName("checkbox1"); for (var i = 0; list[i]; ++i) { if ( (sum & list[i].val()) == list[i].val() ) { $(this).prop('checked', true); } }
<table id="div_table"> <tbody> <tr> <td> <input type="checkbox" class="checkbox1" id="candy" value="2" /> </td> <td>Candy</td> </tr> <tr> <td> <input type="checkbox" class="checkbox1" id="food" value="1" /> </td> <td>Food</td> </tr> <tr> <td> <input type="checkbox" class="checkbox1" id="water" value="4" /> </td> <td>Water</td> </tr> </tbody> </table>
Thank guys. 谢谢大家
current checkbox dom object lies in list[i]
in for loop. 当前复选框dom对象位于for循环的list[i]
中。 you can convert the object to jquery and then set property checked to true: 您可以将对象转换为jquery,然后将属性设置为true:
for (var i = 0; list[i]; ++i){
var currentchckbox = $(list[i]);
if ((sum & currentchckbox.val()) == currentchckbox .val() ) {
currentchckbox.prop('checked', true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.