Javascript .checked=true 不适用于 jquery $ 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3334935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.checked=true not working with jquery $ function
提问by Anish
i want to select a checkbox when a button is clicked.
我想在单击按钮时选择一个复选框。
<form action="" method="post" id="form2">
<input type="checkbox" id="checkone" value="one" name="one" />
<input type="button" value="Click me" id="buttonone"/>
</form>
when i tried the following, the checkbox was not getting selected
当我尝试以下操作时,复选框没有被选中
$('#buttonone').click(function() {
$('#checkone').checked=true;
});
then i tried:
然后我尝试:
$('#buttonone').click(function() {
document.getElementById('checkone').checked=true;
});
this time the checkbox got selected. why isn't it getting selected with the jquery $ function?
这次复选框被选中了。为什么它没有被 jquery $ 函数选中?
回答by Anurag
Try
尝试
$('#checkone').attr('checked', true);
or
或者
$('#checkone').get(0).checked = true;
or
或者
$('#checkone')[0].checked = true; // identical to second example
The reason your first code didn't work is because you were trying to set the checkedproperty on a jQuery object which will have no visible effect as it only works on the native DOM object.
您的第一个代码不起作用的原因是因为您试图checked在 jQuery 对象上设置属性,该属性不会产生可见效果,因为它仅适用于本机 DOM 对象。
By calling get(0)or accessing the first item [0], we retrieve the native DOM element and can use it normally as in your second example. Alternatively, set the checkedattribute using jQuery's attrfunction which should work too.
通过调用get(0)或访问第一个 item [0],我们可以检索原生 DOM 元素,并且可以像在第二个示例中一样正常使用它。或者,checked使用 jQuery 的attr函数设置属性,它也应该可以工作。
回答by Nick Craver
You need to use .attr()for the jQuery object, like this:
您需要使用jQuery 对象,如下所示:.attr()
$('#buttonone').click(function() {
$('#checkone').attr('checked', true);
});
But it's better to do it the DOM way, like this:
但最好以 DOM 方式进行,如下所示:
$('#buttonone').click(function() {
$('#checkone')[0].checked = true; //get the DOM element, .checked is on that
});
Or, completely without jQuery:
或者,完全没有 jQuery:
document.getElementById('buttonone').onclick = function() {
document.getElementById('checkone').checked = true;
};
回答by bozdoz
None of these answers worked for me because I incorrectly had multiple radios with the same name attributes:
这些答案都不适合我,因为我错误地拥有多个具有相同名称属性的收音机:
<div id="group-one">
<input type="radio" name="groups" value="1" checked="checked" />
<input type="radio" name="groups" value="2" />
</div>
<div id="group-two">
<input type="radio" name="groups" value="1" checked="checked" />
<input type="radio" name="groups" value="2" />
</div>
Javascript won't recognize the checked attribute (obviously). This was a result of using includeto add a similar section of HTML multiple times. Obviously, clicking on a radio button will uncheck the radio toggles with the same name.
Javascript 不会识别被选中的属性(显然)。这是include多次使用添加类似 HTML 部分的结果。显然,单击单选按钮将取消选中具有相同名称的单选按钮。
Here's a jsfiddle to show that two radio elements can have the attribute checkedbut only the last one is actually checked:
这是一个 jsfiddle,显示两个单选元素可以具有该属性,checked但实际上只检查最后一个:
http://jsfiddle.net/bozdoz/5ecq8/
http://jsfiddle.net/bozdoz/5ecq8/
Again, pretty obvious, but possibly something to watch out for: remove idand nameattributes from files that you intend to include into other files multiple times.
同样,非常明显,但可能需要注意:从您打算多次包含到其他文件中的文件中删除id和name属性。
回答by chetan tyagi
try this
尝试这个
$('#buttonone').click(function() {
$('#checkone').prop('checked', true);
});
回答by Stefanvds
try
尝试
$('#checkone').attr('checked', true);
cleary googling for "jquery check a checkbox" was the way to go
清晰的谷歌搜索“jquery check a checkbox”是要走的路
回答by Matt Derrick
Or you could simply do
或者你可以简单地做
$('#buttonone').click(function() {
$('#checkone')[0].checked=true;
});
It is because ".checked" is not part of jQuery and you are trying to use it on a jQuery object. If you index a jQuery object at [0] you get the raw Javascript object which ".checked" exists on.
这是因为“.checked”不是 jQuery 的一部分,而您正试图在 jQuery 对象上使用它。如果你在 [0] 处索引一个 jQuery 对象,你会得到“.checked”存在的原始 Javascript 对象。
More here: http://phrappe.com/javascript/convert-a-jquery-object-to-raw-dom-object/
更多信息:http: //phrappe.com/javascript/convert-a-jquery-object-to-raw-dom-object/
回答by RaYell
Try
尝试
$('#checkone').attr('checked', true);
You don't have direct access to DOM object properties because jQuery operates on collections ($(selector)is an array). That's why you have functions defined to manipulate the contents of the returned elements.
您无法直接访问 DOM 对象属性,因为 jQuery 对集合进行操作($(selector)是一个数组)。这就是为什么您定义了函数来操作返回元素的内容。

