简体   繁体   English

选中/取消选中复选框jQuery

[英]Check/Uncheck checkbox jQuery

I have a situation where I get the following in JavaScript: 我有一种情况,我在JavaScript中得到以下信息:

$('<input type="checkbox" name="checkbox1" value="12345">').prop('checked',true);

The above doesn't check the checkbox at all. 上面根本没有选中该复选框。 Also tried: 还尝试了:

$('<input type="checkbox" name="checkbox1" value="12345">').attr('checked','checked');

When I do this, I get the value: 当我这样做时,我得到的价值是:

$('<input type="checkbox" name="checkbox1" value="12345">').val();

The following is contained in an array: 数组中包含以下内容:

<input type="checkbox" name="checkbox1" value="12345">

So I usually call above as: 所以我通常在上面这样称呼:

$(data[0]).val();

Why doesn't checking/unchecking using prop or attr work? 为什么不使用prop或attr进行检查/取消检查呢?

The checkbox IS checked. 复选框已选中。

var elem = $('<input type="checkbox" name="checkbox1" value="12345">').prop('checked',true);
console.log(elem.prop("checked"));  //true
console.log(elem[0]);   //<input type="checkbox" name="checkbox1" value="12345">

The DOM does not magically add the property as an attribute when you inspect it. 在检查时,DOM不会神奇地将属性添加为属性。 If you want the HTML to update, you would have to add an attribute 如果要更新HTML,则必须添加一个属性

var elem = $('<input type="checkbox" name="checkbox1" value="54321">').attr("checked","checked");
console.log(elem.prop("checked"));  //true
console.log(elem[0]);  //<input type="checkbox" name="checkbox1" value="12345" checked="checked">

JSFiddle JSFiddle

$('#IDOFCHECKBOX').prop('checked',true);

check this for .prop() 检查此.prop()

check this for id selector 检查此ID选择器

for select by name you can use 通过名称选择可以使用

$('input[type=checkbox][name="checkbox1"]').prop('checked', true)

It certainly is in jQuery 1.6+ 肯定在jQuery 1.6+中

Use the new .prop() function: 使用新的.prop()函数:

$('.myCheckbox').prop('checked', true); $('。myCheckbox')。prop('checked',true); $('.myCheckbox').prop('checked', false); $('。myCheckbox')。prop('checked',false);

jQuery 1.5 and below jQuery 1.5及以下

The .prop() function is not available, so you need to use .attr(). .prop()函数不可用,因此您需要使用.attr()。

To check the checkbox (by setting the value of the checked attribute) do 要选中复选框(通过设置被选中属性的值),请执行

$('.myCheckbox').attr('checked','checked'); $('。myCheckbox')。attr('checked','checked');

and for un-checking (by removing the attribute entirely) do 并取消选中(通过完全删除属性)来执行

$('.myCheckbox').removeAttr('checked'); $('。myCheckbox')。removeAttr('checked');

Any version of jQuery 任何版本的jQuery

If you're working with just one element, it will always be fastest to use DOMElement.checked = true. 如果仅使用一个元素,则使用DOMElement.checked = true总是最快的。 The benefit to using the .prop() and .attr() functions is that they will operate on all matched elements. 使用.prop()和.attr()函数的好处是它们将对所有匹配的元素进行操作。

// Assuming an event handler on a checkbox if (this.checked) //假设(this.checked)复选框上有一个事件处理程序

You should use a proper jQuery selector, eg: 您应该使用适当的jQuery选择器,例如:

$('#checkboxId').prop('checked',true);

Here's a list of the existing selectors: 以下是现有选择器的列表:

http://api.jquery.com/category/selectors/ http://api.jquery.com/category/selectors/

Most of the CSS selectors will work, eg: 大多数CSS选择器都可以使用,例如:

  • .class to select Classes .class选择类别
  • #id to select by Ids 通过ID选择的#id
  • > to select direct childrens >选择直系儿童
  • [attr=value] to select by attribute value [attr=value]按属性值选择

Any one of this could check the check box for you 其中任何一项都可以为您选中该复选框

$('#chk1').prop('checked','checked')

$('#chk1').prop('checked',true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM