问题陈述很简单。我需要看看用户是否从无线电组中选择了一个单选按钮。组中的每个单选按钮共享相同的id。

问题是我无法控制表单是如何生成的。下面是单选按钮控件代码的样例代码:

<input type="radio" name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >

除此之外,当单选按钮被选中时,它不会添加一个“checked”属性到控件中,只是文本检查(我猜只是没有值的属性检查)。下面是选定的无线电控件的外观

<input type="radio" checked name='s_2_1_6_0' value='Mail copy to my bill to address' id = "InvCopyRadio" onchange = 'SWESubmitForm(document.SWEForm2_0,s_4,"","1-DPWJJF")' style="height:20;width:25" tabindex=1997 >

有人能帮助我用jQuery代码,可以帮助我得到选中单选按钮的值吗?


当前回答

另一种理解…工作:

HTML代码:

 <input type="radio" name="active_status" class="active_status" value="Hold">Hold 
 <input type="radio" name="active_status" class="active_status" value="Cancel">Cancel 
 <input type="radio" name="active_status" class="active_status" value="Suspend">Suspend

Jquery代码:

$(document).on("click", ".active_status", function () {
 var a = $('input[name=active_status]:checked').val();  
 (OR)   
 var a = $('.active_status:checked').val();
 alert(a);
});

其他回答

请参阅下面的工作示例,其中包含与不同部分相关联的无线电组集合。您的命名方案很重要,但理想情况下,您应该尝试对输入使用一致的命名方案(特别是当它们位于像这里这样的部分时)。

$('#submit').click(function(){ var section = $('input:radio[name="sec_num"]:checked').val(); var question = $('input:radio[name="qst_num"]:checked').val(); var selectedVal = checkVal(section, question); $('#show_val_div').text(selectedVal); $('#show_val_div').show(); }); function checkVal(section, question) { var value = $('input:radio[name="sec'+section+'_r'+question+'"]:checked').val() || "Selection Not Made"; return value; } * { margin: 0; } div { margin-bottom: 20px; padding: 10px; } h5, label { display: inline-block; } .small { font-size: 12px; } .hide { display: none; } #formDiv { padding: 10px; border: 1px solid black; } .center { display:block; margin: 0 auto; text-align:center; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="center"> <div> <h4>Section 1</h4> <h5>First question text</h5> <label class="small"><input type="radio" name="sec1_r1" value="(1:1) YES"> YES</label> <label class="small"><input type="radio" name="sec1_r1" value="(1:1) NO"> NO</label> <br/> <h5>Second question text</h5> <label class="small"><input type="radio" name="sec1_r2" value="(1:2) YES"> YES</label> <label class="small"><input type="radio" name="sec1_r2" value="(1:2) NO"> NO</label> <br/> <h5>Third Question</h5> <label class="small"><input type="radio" name="sec1_r3" value="(1:3) YES"> YES</label> <label class="small"><input type="radio" name="sec1_r3" value="(1:3) NO"> NO</label> </div> <div> <h4>Section 2</h4> <h5>First question text</h5> <label class="small"><input type="radio" name="sec2_r1" value="(2:1) YES"> YES</label> <label class="small"><input type="radio" name="sec2_r1" value="(2:1) NO"> NO</label> <br/> <h5>Second question text</h5> <label class="small"><input type="radio" name="sec2_r2" value="(2:2) YES"> YES</label> <label class="small"><input type="radio" name="sec2_r2" value="(2:2) NO"> NO</label> <br/> <h5>Third Question</h5> <label class="small"><input type="radio" name="sec2_r3" value="(2:3) YES"> YES</label> <label class="small"><input type="radio" name="sec2_r3" value="(2:3) NO"> NO</label> </div> <div> <h4>Section 3</h4> <h5>First question text</h5> <label class="small"><input type="radio" name="sec3_r1" value="(3:1) YES"> YES</label> <label class="small"><input type="radio" name="sec3_r1" value="(3:1) NO"> NO</label> <br/> <h5>Second question text</h5> <label class="small"><input type="radio" name="sec3_r2" value="(3:2) YES"> YES</label> <label class="small"><input type="radio" name="sec3_r2" value="(3:2) NO"> NO</label> <br/> <h5>Third Question</h5> <label class="small"><input type="radio" name="sec3_r3" value="(3:3) YES"> YES</label> <label class="small"><input type="radio" name="sec3_r3" value="(3:3) NO"> NO</label> </div> </div> <div id="formDiv" class="center"> <form target="#show_val_div" method="post"> <p>Choose Section Number</p> <label class="small"> <input type="radio" name="sec_num" value="1"> 1</label> <label class="small"> <input type="radio" name="sec_num" value="2"> 2</label> <label class="small"> <input type="radio" name="sec_num" value="3"> 3</label> <br/><br/> <p>Choose Question Number</p> <label class="small"> <input type="radio" name="qst_num" value="1"> 1</label> <label class="small"> <input type="radio" name="qst_num" value="2"> 2</label> <label class="small"> <input type="radio" name="qst_num" value="3"> 3</label> <br/><br/> <input id="submit" type="submit" value="Show Value"> </form> <br/> <p id="show_val_div"></p> </div> <br/><br/><br/>

$("#radioID") // select the radio by its id
    .change(function(){ // bind a function to the change event
        if( $(this).is(":checked") ){ // check if the radio is checked
            var val = $(this).val(); // retrieve the value
        }
    });

确保将其包装在DOM就绪函数中($(function(){…});美元(文档)时的功能 (){...});).

HTML标记:

<div class="form-group">
  <input id="rdMale" type="radio" class="clsGender" value="Male" name="rdGender" /> Male
  <input id="rdFemale" type="radio" class="clsGender" value="Female" name="rdGender" /> Female
 </div>

通过单选按钮Id获取所选值:

 $("input[name='rdGender']").click(function () {
     alert($("#rdMale").val());
});

通过单选按钮Classname获取值

 $(".clsGender").click(function () {
       alert($(this).val());
            //or
      alert($(".clsGender:checked").val());
   });

按名称获取值

   $("input[name='rdGender']").click(function () {
          var rdVaule = $("input[name='rdGender']:checked").val();
          alert(rdVaule);
   });

对于多个单选按钮,您必须在单选按钮标签上放置相同的name属性。 例如;

<input type="radio" name"gender" class="select_gender" value="male">
<input type="radio" name"gender" class="select_gender" value="female">

一旦你有了单选选项,现在你可以像下面这样使用jQuery代码来获得选中/选中的单选选项的值。

$(document).on("change", ".select_gender", function () {
   console.log($(this).val());    // Here you will get the current selected/checked radio option value
});

注意:使用$(document)是因为如果单选按钮是在DOM 100%加载后创建的,那么你需要它,因为如果你不使用$(document),那么jQuery将不知道新创建的单选按钮的范围。像这样调用jQuery事件是一个很好的实践。

使用以下代码

$('input[name=your_radio_button_name]:checked').val();

请注意,值属性应该被定义,所以可以得到“男性”或“女性”在你的结果。

<div id='div_container'>
<input type="radio" name="Gender" value="Male" /> Male <br />
<input type="radio" name="Gender" value="Female" /> Female
</div>