简体   繁体   English

单选按钮被选中的jQuery

[英]Radio button is checked jquery

I have a series of radio buttons inside a form which is in the body of a bootstrap 4 modal. 我在bootstrap 4模态主体中的表单中有一系列单选按钮。 I'm trying to see which one is checked, which should be a very simple thing to do but I'm struggling with it today and don't know what I'm doing wrong. 我试图查看已检查的那一项,这应该是一件非常简单的事情,但是我今天正在为此而苦苦挣扎,不知道我在做什么错。

here is the HTML. 这是HTML。

<div class="card-body">
  <div class="custom-control custom-radio">
    <input type="radio" id="tdeeActivitySedentary" name="customRadioInline3" class="custom-control-input" value="1.2">
    <label class="custom-control-label" for="tdeeActivitySedentary">Sedentary (little or no exercise)</label>
  </div>
  <div class="custom-control custom-radio">
    <input type="radio" id="tdeeActivityLight" name="customRadioInline3" class="custom-control-input" value="1.375">
    <label class="custom-control-label" for="tdeeActivityLight">Light Activity (light exercise 1-3 days per week)</label>
  </div>
  <div class="custom-control custom-radio">
   <input type="radio" id="tdeeActivityMod" name="customRadioInline3" class="custom-control-input" value="1.55">
    <label class="custom-control-label" for="tdeeActivityMod">Moderately Activity (moderate exercise 3-5 days per week)</label>
  </div>
  <div class="custom-control custom-radio">
    <input type="radio" id="tdeeActivityHeavy" name="customRadioInline3" class="custom-control-input" value="1.725">
    <label class="custom-control-label" for="tdeeActivityHeavy">Very Active (hard exercise 5+ days per week)</label>
  </div>
  <div class="custom-control custom-radio">
    <input type="radio" id="tdeeActivityEx" name="customRadioInline3" class="custom-control-input" value="1.9">
    <label class="custom-control-label" for="tdeeActivityEx">Extremely Active (very hard exercise 6+ days per week)</label>
  </div>
</div>

I've tried: 我试过了:

for (var i = 0; i < 7; i++) {
  if ($("input[name='customRadioInline3']").is(':checked')) {
    alert("Checked")
  } 
}

And: 和:

if ($("input[name='customRadioInline3']").is(':checked')) {
  alert("Checked")
} 

the only thing that works is if I put this inside a button click (but I can't do that because I have to check the value before the submit button is executed). 唯一有效的方法是将其放在按钮单击中(但是我不能这样做,因为我必须在执行提交按钮之前检查该值)。

You can check the length of the checked radio button on button click like the following way: 您可以按以下方式检查按钮单击时选中的单选按钮的长度:

 $("#btnClick").click(function(){ if($("input[name=customRadioInline3]:checked").length > 0){ alert("Checked"); } else{ alert("Not checked"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="card-body"> <div class="custom-control custom-radio"> <input type="radio" id="tdeeActivitySedentary" name="customRadioInline3" class="custom-control-input" value="1.2" > <label class="custom-control-label" for="tdeeActivitySedentary">Sedentary (little or no exercise)</label> </div> <div class="custom-control custom-radio"> <input type="radio" id="tdeeActivityLight" name="customRadioInline3" class="custom-control-input" value="1.375"> <label class="custom-control-label" for="tdeeActivityLight">Light Activity (light exercise 1-3 days per week)</label> </div> <div class="custom-control custom-radio"> <input type="radio" id="tdeeActivityMod" name="customRadioInline3" class="custom-control-input" value="1.55"> <label class="custom-control-label" for="tdeeActivityMod">Moderately Activity (moderate exercise 3-5 days per week)</label> </div> <div class="custom-control custom-radio"> <input type="radio" id="tdeeActivityHeavy" name="customRadioInline3" class="custom-control-input" value="1.725"> <label class="custom-control-label" for="tdeeActivityHeavy">Very Active (hard exercise 5+ days per week)</label> </div> <div class="custom-control custom-radio"> <input type="radio" id="tdeeActivityEx" name="customRadioInline3" class="custom-control-input" value="1.9"> <label class="custom-control-label" for="tdeeActivityEx">Extremely Active (very hard exercise 6+ days per week)</label> </div> </div> <input type="button" id="btnClick" value="Click"/> 

Not sure what you want to achieve, but $("input[name='customRadioInline3']") will return all the inputs in an array so you have to iterate them. 不确定要实现什么,但是$("input[name='customRadioInline3']")将返回数组中的所有输入,因此您必须对其进行迭代。

var radios=$("input[name='customRadioInline3']");
for(var i =0; i < radios.length; i++ ) {
    if(radios[i].checked) {
        alert("Checked");
    } 
}

In case you only want the checked one: 如果您只想要选中的内容:

$("input[name='customRadioInline3']:checked")

use val() to get the value: 使用val()获得值:

$("input[name='customRadioInline3']:checked").val()

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

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