简体   繁体   English

检查是否选择了任何收音机或复选框

[英]Check if any radio or checkbox is selected

How would I use jQuery to determine if a checkbox or radio button is checked or not? 我如何使用jQuery来确定是否选中了复选框或单选按钮?

Here is my HTML: 这是我的HTML:

<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form>

<span id="check1">check</span>

<form>
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike<br />
<input type="checkbox" name="vehicle" value="Car" /> I have a car
</form>

<span id="check2">check</span>

Here is some psuedo JavaScript: 这是一些伪的JavaScript:

$("#check1").click(function(){
    if(any radio is selected){
       alert("Please select one radio");
    }
})

$("#check2").click(function(){
    if(any checkbox is selected){
       alert("Please select minimum one checkbox");
    }
})

It is possible in jQuery? 有可能在jQuery?

Live example on jsfiddle: http://jsfiddle.net/BghzK/ Thanks for help! 关于jsfiddle的实例: http//jsfiddle.net/BghzK/感谢您的帮助!

You can make use of the :checked pseudoselector in your selector expression. 您可以在选择器表达式中使用:checked pseudoselector。 Combine this with .length to see how many have been returned. 将此与.length结合使用可以查看返回的数量。 In this case we get all selected radio buttons and see if length is zero, indicating that none have been selected. 在这种情况下,我们将获得所有选定的单选按钮,并查看长度是否为零,表示没有选择任何按钮。

http://jsfiddle.net/mrtsherman/BghzK/2/ http://jsfiddle.net/mrtsherman/BghzK/2/

$("#check1").click(function(){
    if($('input[type=radio]:checked').length == 0){
       alert("Please select one radio");
    }
})

$("#check2").click(function(){
    if($('input[type=checkbox]:checked').length == 0){
       alert("Please select minimum one checkbox");
    }
})

​
if( !$(':radio:checked').length){
    alert('Please select one radio');
}

You will need to use is(":checked") with the selector. 您需要在选择器中使用is(“:checked”)。 Sample code below: 示例代码如下:

 $(function() {
           $("#check1").click(function(){
                if(!($("input[name='sex']").is(":checked"))){
                   alert("Please select one radio");
                }
             })

        $("#check2").click(function(){
             if(!($("input[name='vehicle']").is(":checked"))){
               alert("Please select minimum one checkbox");

            }
        })

        });

use jquery Validation plugin its done automatically. 使用jquery Validation插件自动完成。 For radio button you just need to make the element required in the form. 对于单选按钮,您只需要在表单中创建required的元素。

var rdo = $('input[name="group"]:checked');
var boxes = $('input[type="checkbox"]:checked');

if(rdo.length == 0) 
    alert("Please select one radio");

if(boxes.length == 0)
    alert("Please select minimum one checkbox");

you can try this way this simple 你可以尝试这种方式这么简单

   var chkvalue =   $('input[name="sex"]:checked').val();
    if(chkvalue =="")
    {
     alert("Please checked the radio button");
     return;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM