[英]how to check/uncheck checkbox by using value in query
HTML CODE: HTML代码:
<form id="myform">
<input type='checkbox' name='foo[]' value='1'>
<input type='checkbox' name='foo[]' checked='true' value='2' >
<input type='checkbox' name='foo[]' value='3' >
<input type='checkbox' name='foo[]' checked='true' value='4' >
</form>
JS CODE: JS代码:
$('input[name=foo]:checked').each(function(i,el){
if(clusterVal == 'ALL')
{
/* what do I have to do here? */
}
array.push($(el).val());
});
how to check/uncheck checkbox by using value in query 如何通过使用查询中的值来选中/取消选中复选框
As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes. 从jQuery 1.6开始, .prop()方法提供了一种显式检索属性值的方法,而.attr()则检索属性。
try this ..but this to just demo how to do check and uncheck 试试这个..但这只是演示如何检查和取消选中
$(function(){
var el=$('input:checkbox[name="foo[]:checked"]');
el.each(function()
{
$(this).prop('checked', true);
});
});
Set the checked attribute of the checkbox element to true or false , ie 将checkbox元素的checked属性设置为true或false ,即
el.checked = false;
If you want to do it with jquery, use the attr function an the checked attibute as well 如果要使用jquery进行此操作,请同时使用attr函数和选中的 attibute
Hans is right,
although
But as Anthony pointed out to me, el
will likely be a jQuery object (
.each
method returns jQuery objects in this case. as Anthony pointed out, This isn't always the case.)
el
won't always be a pure dom object. 汉斯是对的,
尽管
但是正如Anthony对我指出的那样, el
可能是一个jQuery对象(在这种情况下
.each
方法返回jQuery对象。正如Anthony所指出的,情况并非总是如此。)
el
不会总是成为纯粹的dom对象。 It can sometimes be a jQuery object, so the checked
property isn't necessarily available. 有时它可以是jQuery对象,因此checked
属性不一定可用。 A couple of ways to deal with those cases: 处理这些情况的几种方法:
$(el).get(0).checked = false;
$(el).removeAttr('checked');//or $(el).attr('checked',false);
$(el).get(0).checked = true;
$(el).attr('checked','checked');
Credit to Anthony for pointing out my thick-headedness on $.each
and .each
's behaviour :) 感谢Anthony指出我在$.each
和.each
的行为上$.each
.each
:)
Use the prop or the attr to do this. 使用道具或属性来执行此操作。 Something like this 像这样
$(el).attr("checked", "checked");
or 要么
$(el).prop("checked", true);
Jquery API Prop() jQuery API Prop()
You can check or uncheck element following 您可以选中或取消选中以下元素
$(element).prop('checked',true); // check
$(element).prop('checked',false); // uncheck
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.