使用 jQuery 通用选择器取消选中单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6121515/
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
Uncheck radio buttons using jQuery general selectors
提问by ilyo
I have the following form (it is dynamically generated and I have a few copies of it named form-0 to form-3)
我有以下表单(它是动态生成的,我有一些名为 form-0 到 form-3 的副本)
<form id="form-0" name="0">
<b>what is bla?</b><br>
<div class="ansN" id="ans0"><input type="radio" name="answerN" value="0"> aaa </div>
<div class="ansN" id="ans1"><input type="radio" name="answerN" value="1"> bbb </div>
<div class="ansN" id="ans2"><input type="radio" name="answerN" value="2"> ccc </div>
<div class="ansN" id="ans3"><input type="radio" name="answerN" value="3"> ddd </div>
<input type="submit" value="Submit your answer">
</form>
(ignore the divs, they are used for some other purpose)
(忽略 div,它们用于其他目的)
Itry to create an uncheck function so that will work on all the radio-buttons on the page, so I played with the console and tried to deselect them using this:
我尝试创建一个取消选中功能,以便在页面上的所有单选按钮上工作,所以我玩了控制台并尝试使用以下方法取消选择它们:
$('form[id^="form-"]').find("input:radio:checked").attr('checked', false);
$('form[id^="form-"]').find("input:radio:checked").removeAttr("checked");
So the selector did find all the checked buttons, but it didn't uncheck them, why?
when I use direct JS call: document.forms[0].answerN[3].checked = false
it works fine,
so I can use it inside a loop but I try to avoid loops here. Is there a better way?
所以选择器确实找到了所有选中的按钮,但它没有取消选中它们,为什么?当我使用直接 JS 调用时:document.forms[0].answerN[3].checked = false
它工作正常,所以我可以在循环中使用它,但我尽量避免在这里出现循环。有没有更好的办法?
My goal is to have an uncheck function, so the second click on the radio button itself will uncheck it
我的目标是具有取消选中功能,因此第二次单击单选按钮本身将取消选中它
回答by Sylvain
If you are using jQuery 1.6, you could use prop()
instead of attr()
:
如果您使用的是 jQuery 1.6,则可以使用prop()
代替attr()
:
$('form[id^="form-"]').find("input:radio:checked").prop('checked',false);
// OR (not recommended)
$('form[id^="form-"]').find("input:radio:checked").removeProp('checked');
However, it is not advised to use the removeProp
variant, according to the documentation:
但是,removeProp
根据文档,不建议使用该变体:
Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.
不要使用此方法删除本机属性,例如checked、disabled或selected。这将完全删除该属性,并且一旦删除,就无法再次添加到元素中。使用 .prop() 将这些属性设置为 false。
( thanks @noah1989 for pointing this out )
(感谢@noah1989 指出这一点)
attr()
retrieves the value that have been set in the source code, and prop()
retrieves the current, dynamically set value of the attribute.
attr()
检索已在源代码中prop()
设置的值,并检索属性的当前动态设置值。
You can see a jsFiddle example here
你可以在这里看到一个jsFiddle 例子
回答by inquam
You should be able to uncheck using
您应该可以取消选中使用
$('input[name=foo]').attr('checked', false);
My initial guess is that you get a collection of radio buttons from your find
. Thaus you should use .each()
to loop them and set the checked
attribute to false on each individual one.
我最初的猜测是您从find
. 您应该使用它.each()
来循环它们并将checked
每个单独的属性设置为 false。
Something like this (not testet, so might be a syntax error)
像这样的东西(不是 testet,所以可能是语法错误)
$('form[id^="form-"]').find("input:radio:checked").each(
function() {
$(this).attr('checked', false);
}
);
回答by Amol Gholap
<input type="radio" id="ra1" onclick="change('ra1')" onmousedown="getvalue('ra1')"/>
function getvalue(id){
ischecked = document.getElementById(id).checked;
}
function change(id){
if(ischecked == false)
document.getElementById(id).checked=true;
else
document.getElementById(id).checked=false;
}