使用 jquery 验证单选按钮?

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

Validate radio buttons with jquery?

jqueryforms

提问by undefined

How do I validate these two radio buttons using jQuery? One button should be checked:

如何使用 jQuery 验证这两个单选按钮?应该检查一个按钮:

<input type="radio" name="muuttuminen" value="Kylla" /> Kyll?
<input type="radio" name="muuttuminen" value="Ei"/>Ei

I know how to validate regular text input fields, but I'm wondering about this one.

我知道如何验证常规文本输入字段,但我想知道这个。

     $(function() {
        $("#submit").click(function(){
 //validation part
          var dataString = $('#service-test').serialize();
          //alert (dataString);return false;
         $.ajax({
        type: "POST",
        url: "../quiz.php",
        data: dataString,
        dataType: "text",
        error: function(){ alert ('Nyt jotakin meni kyll? pahemman kerran pieleen. Yrit? uudelleen?');
     },
        success: 
        function(data) {
     $("#resultcontainer").html(data);
     $("#service-test").hide();

       }

      });
      return false;

        });
      });               

回答by undefined

You can use :checkedselector and the lengthproperty:

您可以使用:checked选择器和length属性:

    $(".submit").click(function() {
       if ($('input[name="muuttuminen"]:checked').length == 0) {
         alert('please...');
         return false; } 
          else {
          $.ajax({
           //...
         })
       }
       return false;
    }); 

回答by Adil

You can do it like give below.

你可以像下面给出的那样做。

Live Demo

现场演示

$(function() {
    $("#submit").click(function() {     
      if($('input[type=radio][name=muuttuminen]:checked').length == 0)
      {
         alert("Please select atleast one");
         return false;
      }
      else
      {
          alert("radio button selected value: ");
      }      
    });
});

回答by Paul Aldred-Bann

You can do it this way, jsFiddle example:

你可以这样做,jsFiddle 例子

$(function() {
    $("input[type=submit]").click(function() {
        var atLeastOneChecked = false;

        $("input[type=radio]").each(function() {
            if ($(this).attr("checked") == "checked") {
                atLeastOneChecked = true;
            }
        });

        if (!atLeastOneChecked) {
            $("#checked").text("Check one");
        } else {
            $("#checked").text("One is checked");
        }
    });
});?

This is a generalised example that will check ALL radio buttons on the page, you might not want this so it may be worth putting your radio buttons in a group and checking the group this way:

这是一个通用示例,将检查页面上的所有单选按钮,您可能不想要这个,因此可能值得将您的单选按钮放在一个组中并以这种方式检查该组:

$("#inputgroup").find("input[type=radio]").each(function() {
    // do the validation here
});

The code to check these two specific buttons would be:

检查这两个特定按钮的代码是:

$("input[type=radio][name=muuttuminen]").each(function() {
    // do the validation here
});

回答by Johan

Your code is pretty incomplete but something like

您的代码非常不完整,但类似于

var valid = false;

$('#yourForm').find(':input:radio').each(function(){
    if($(this).prop('checked'))
        valid = true;
});

if(!valid)
    return false;

回答by Vap0r

You can use the checked pseudo-class. Here is a fiddle of what you might use for form submission. http://jsfiddle.net/WhPcj/

您可以使用已检查的伪类。这是您可能用于表单提交的小提琴。 http://jsfiddle.net/WhPcj/