单选按钮未在 jQuery 中检查

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

Radio buttons not checked in jQuery

jquery

提问by kd7iwp

I have this line of code for page load:

我有这行代码用于页面加载:

if ($("input").is(':checked')) {

and it works fine when the radio button input is checked. However, I want the opposite. Something along the lines of

检查单选按钮输入时,它可以正常工作。然而,我想要相反的。类似的东西

if ($("input").not(.is(':checked'))) {

so that my if statement runs when none of the radiobuttons are selected. What is the best way to go about this?

以便我的 if 语句在没有选择任何单选按钮时运行。解决这个问题的最佳方法是什么?

回答by singpolyma

if ( ! $("input").is(':checked') )

Doesn't work?

不起作用?

You might also try iterating over the elements like so:

您也可以尝试像这样迭代元素:

var iz_checked = true;
$('input').each(function(){
   iz_checked = iz_checked && $(this).is(':checked');
});
if ( ! iz_checked )

回答by Klemen Slavi?

if ($("input").is(":not(:checked)"))

AFAIK, this should work, tested against the latest stable jQuery (1.2.6).

AFAIK,这应该有效,针对最新的稳定 jQuery (1.2.6) 进行了测试。

回答by Ionu? Staicu

If my firebug profiler work fine (and i know how to use it well), this:

如果我的萤火虫分析器工作正常(并且我知道如何很好地使用它),则:

$('#communitymode').attr('checked')

is faster than

$('#communitymode').is('checked')

You can try on this page :)

您可以在此页面上尝试:)

And then you can use it like

然后你可以像这样使用它

if($('#communitymode').attr('checked')===true) { 
// do something
}

回答by yevgeny

$("input").is(":not(':checked')"))

This is jquery 1.3, mind the ' and " signs!

这是 jquery 1.3,注意 ' 和 " 符号!

回答by Antwan

$('input:radio[checked=false]');

this will also work

这也行

input:radio:not(:checked)

or

或者

:radio:not(:checked)

回答by HaxxanRaxa

(!$('#radio').is(':checked'))

This worked for me

这对我有用

回答by Lapenkov Vladimir

This works too. It seems shortest working notation: !$('#selector:checked')

这也有效。似乎最短的工作符号: !$('#selector:checked')