使用 jQuery 检查验证单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3780040/
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
Validating a radio button is checked with jQuery
提问by Will
On my form I havea set of radio buttons. Here's the mark up:
在我的表单上,我有一组单选按钮。这是标记:
<div class="optionHolder">
<p class="optionName">Format</p>
<div class="option checked">
<input type="radio" name="fileType" value="avi" />
<img src="images/avi.png" alt="" />
<label>AVI</label>
</div>
<div class="option">
<input type="radio" name="fileType" value="mov" />
<img src="images/mov.png" alt="" />
<label>MOV</label>
</div>
<div class="option">
<input type="radio" name="fileType" value="mp4" />
<img src="images/mp4.png" alt="" />
<label>MP4</label>
</div>
<div class="option">
<input type="radio" name="fileType" value="mp3" />
<img src="images/mp3.png" alt="" />
<label>MP3</label>
</div>
</div>
When the form is submitted I want to check that one of them is checked. What's the best way to go about this? I was thinking of looping through them all and making a flag to set if one of them is checked, and then check the flag after the loop and if it's false throw an error.
提交表单后,我想检查其中一个是否被选中。解决这个问题的最佳方法是什么?我正在考虑遍历它们并制作一个标志来设置是否检查了其中一个,然后在循环后检查标志,如果它是错误的则抛出一个错误。
Any help is appreciated, cheers.
任何帮助表示赞赏,干杯。
回答by Sarfraz
You can use the length
and equal attribute selectorwith :checked
filter selector like this:
您可以像这样将length
和equal 属性选择器与:checked
过滤器选择器一起使用:
if ($("input[name='fileType']:checked").length > 0){
// one ore more checkboxes are checked
}
else{
// no checkboxes are checked
}
回答by Praveen Prasad
demo
演示
var isChecked = jQuery("input[name=fileType]:checked").val();
回答by ICodeForCoffee
Try the jQuery Validationplugin. It can do a lot for you and be really useful for lots of different forms. If you want to do it very simply:
试试jQuery 验证插件。它可以为您做很多事情,并且对许多不同的形式都非常有用。如果您想非常简单地做到这一点:
if($("input[name=fileType]:checked").length > 0) {
//Is Valid
}
回答by Naveed
Try:
尝试:
var checkbox = $("input[@name='fileType']:checked");
if( checkbox.length > 0 ) {
alert( checkbox.val() ); // checkbox value
} else {
alert('Please select a format'); // error
}
回答by David Aguirre
Really old, I know. If a radio selection is not selected it returns as 'undefined', not '0'. In my example I declare a variable with the value of the radio buttons. If said value is undefined, the javascript returns false.
真的老了,我知道。如果未选择无线电选择,则返回“未定义”,而不是“0”。在我的示例中,我声明了一个带有单选按钮值的变量。如果未定义该值,则 javascript 返回 false。
gender = $('input[name=gender]:checked').val();
if(typeof gender === 'undefined'){
alert('Do not move on');
$('input[name=gender]').css('box-shadow', '0 0 2px 0 red');
return false;
}
回答by janosrusiczki
I think $('input[name=fileType]:checked').length
will do the trick.
我认为$('input[name=fileType]:checked').length
会做的伎俩。
回答by Andy Rose
You could check to see if the checked radio button name returns a value in jQuery:
您可以检查选中的单选按钮名称是否在 jQuery 中返回一个值:
if($("input[@name='fileType']:checked").val() != null){
// button checked
}