jQuery-遍历所有选中的单选按钮 [英] Jquery - Iterate through all checked radio buttons

查看:258
本文介绍了jQuery-遍历所有选中的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下形式的表格:

I have a form which is similar like the one below:

<form id="myForm">
      Question 1:<br>
      <input type="radio" name="question1" value="Yes"> Yes
      <input type="radio" name="question1" value="No"> No
      <br>
      Question 2:<br>
      <input type="radio" name="question2" value="Yes"> Yes
      <input type="radio" name="question2" value="No"> No
      <br>
      Question 3:<br>
      <input type="radio" name="question3" value="Yes"> Yes
      <input type="radio" name="question3" value="No"> No
      <br>
      <input type="submit" value="Submit" name="submit">
</form>

我想使用jQuery从所有问题中获取所有选定的单选按钮值,如果所有值均等于是",它将警告成功,否则将警告失败.

I want to get all the selected radio button values from all the questions using jQuery and if all values is equal to "yes", it will alert success else it will alert fail.

这是我写的jQuery:

This is the jQuery I wrote:

$(document).ready(function(){
   $("#myForm input[type=radio]:checked").each(function() {
       var value = $(this).val();
   });
});

推荐答案

您可以检查是否未通过广播检查获得否,否则结果为失败,否则为成功.

You can check if you ever get no with radio checked then result is fail else success.

实时演示

Live Demo

result = "success";
$("#myForm input[type=radio]:checked").each(function() {
  if(this.value == "No" && this.checked == true)
  {
     result = "fail";
     return false;
  }
});
alert(result);  

这篇关于jQuery-遍历所有选中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆