Javascript 如何使用 JQuery 检查输入单选按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2240871/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:25:31  来源:igfitidea点击:

How do I make an input radio button checked...using JQuery?

javascriptjquery

提问by TIMEX

<input type="radio" name="sort" value="2" id="myradio">

How do I make this checked in JQUERY?

我如何在 JQUERY 中检查它?

回答by Jage

$('#idOfMyRadio').attr('checked',true);

回答by Tim Down

Really, there's no need for jQuery here: it will be slower, introduce a needless framework dependency and increase the possibility of error. The DOM method for this is also as clear and readable as it could be, and works in all the major browsers back to the 1990s:

真的,这里不需要jQuery:它会更慢,引入不必要的框架依赖并增加出错的可能性。用于此操作的 DOM 方法也尽可能清晰易读,并且适用于 1990 年代以来的所有主要浏览器:

document.getElementById("myradio").checked = true;

If you must use jQuery then I'd stop at just using it to get hold of the element:

如果您必须使用 jQuery,那么我将停止使用它来获取元素:

$("#myradio")[0].checked = true;

回答by Jay Corbett

In addition to using id...

除了使用身...

$('input[name="sort"]').attr('checked',true);
$('input[value="2"]').attr('checked',true);

回答by Atif Tariq

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