简体   繁体   English

如何检测是否已使用jQuery选择了组中的一个单选按钮

[英]How to detect if a one radio button in a group has been selected with jQuery

I'm 95% there in writing an HTML5 'required' attribute fallback, I'm having a small issue and I've come to the end of the road in my knowledge! 我有95%的人在编写HTML5“必需”属性后备广告,遇到了一个小问题,据我所知,这已经走到了尽头!

What works: Detecting 'required' attributes, looping through and alerting the user in several ways (onsubmit and entering data into the field). 可行的方法:检测“必需”属性,以几种方式遍历并警告用户(提交和将数据输入字段)。

Problem: I'm taking the form one step further and want to make checkboxes and radio buttons required as well. 问题:我正在将表单进一步发展,并且还希望使复选框和单选按钮成为必需。 By doing this, I need to add a required attribute to each radio/checkbox. 通过这样做,我需要向每个单选框/复选框添加必需的属性。 I need to find out how to differentiate the group of buttons, as currently you need to tick/untick both sets of buttons for the form to validate and let you submit. 我需要找出如何区分按钮组的方法,因为当前您需要勾选/取消选中两组按钮,以便表单进行验证并提交。 So in a nutshell, I have three required radios for example, each will need to be ticked, how can I detect whilst looping through the inputs that one of the required radios/checked is ticked - I assume this would be done by matching the 'name' attribute, and if none in the name group are selected then alert just one error. 简而言之,例如,我有三个必需的无线电,每个都需要被打勾,如何在循环输入的同时检测到一个必需的无线电/选中的一个被打勾-我假设这可以通过匹配'名称”属性,如果在名称组中未选择任何属性,则仅警告一个错误。

Here's the state of my loop, you can see I detect the input type as well, just unsure of the next steps forward. 这是我的循环状态,您可以看到我也检测到输入类型,只是不确定下一步的工作。 a jsFiddle is also below if anyone would be kind enough to help out. 如果有人愿意帮助的话,jsFiddle也在下面。 Many thanks. 非常感谢。

// loop through class name required
    $('.required').each(function () {

        // this
        var self = $(this)

        // check shorthand if statement for input[type] detection
        var checked = (self.is(':checkbox') || self.is(':radio')) ? self.is(':not(:checked)') : false

        // run the empty/not:checked test
        if (self.val() === '' || checked) {

            // show error if the values are empty still (or re-emptied)
            // this will fire after it's already been checked once
            self.siblings('.form-error').show()

            // stop form submitting
            e.preventDefault()

            // if it's passed the check
        } else {

            // hide the error
            self.siblings('.form-error').hide()

        }

    })

Here's my jsFiddle: http://jsfiddle.net/zykF9/ 这是我的jsFiddle: http : //jsfiddle.net/zykF9/

With class selectors you could archive it http://jsbin.com/owokuw/1/edit 使用类选择器,您可以将其存档http://jsbin.com/owokuw/1/edit

UPDATE

to make easier to understand, here a update: 为了更容易理解,请在此处进行更新:

  <input type="radio" name="myradio"  class="myradio" />
<input type="radio" name="myradio"  class="myradio" />
<input type="radio" name="myradio"  class="myradio" />
<input type="radio" name="myradio"  class="myradio" />
  <input type="hidden"   id="selectedRadioYes" />
  <input type="button" id="botton" value="Botton" />

Jquery jQuery的

$(".myradio").click(function(){
$("#selectedRadioYes").val(1);
});




$("#botton").click(function(){
  if($("#selectedRadioYes").val() === "1")
  alert("you can go on"); 
  else
   alert("need select one radio");

});

http://jsbin.com/owokuw/2/edit http://jsbin.com/owokuw/2/edit

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

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