问题陈述很简单。我需要看看用户是否从无线电组中选择了一个单选按钮。组中的每个单选按钮共享相同的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代码,可以帮助我得到选中单选按钮的值吗?


当前回答

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

$('#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/>

其他回答

    <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 >

$(function() {
      $("#submit").click(function() {
        alert($('input[name=s_2_1_6_0]:checked').val());
      });
    });`

多组单选按钮隐藏值到数组

var arr = []; function r(n) { var section = $('input:radio[name="' + n + '"]:checked').val(); arr[n] = section; console.log(arr) } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="radio" onchange="r('b1')" class="radioID" name="b1" value="1">1 <input type="radio" onchange="r('b1')" class="radioID" name="b1" value="2" >2 <input type="radio" onchange="r('b1')" class="radioID" name="b1" value="3">3 <br> <input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="4">4 <input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="5" >5 <input type="radio" onchange="r('b2')" class="radioID2" name="b2" value="6">6

只使用。

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

就是这么简单。

解释这个简单话题的最好方法是给出简单的例子和参考链接:-

在下面的例子中,我们有两个单选按钮。如果用户选择任何单选按钮,我们将在警报框中显示所选单选按钮值。

Html:

<form id="Demo">
<input type="radio" name="Gender" value="Male" /> Male <br />
<input type="radio" name="Gender" value="Female" /> Female<br />
<input type="radio" name="Gender" value="others" /> others <br />
</form>

jquery: -

 $(document).ready(function(){
        $('#Demo input').on('change', function() {
            alert($('input[name="Gender"]:checked', '#Demo').val());
        });
    });

我不是javascript的人,但我在这里找到了搜索这个问题。对于谁谷歌它和发现这里,我希望这有助于一些。所以,如果我们有一个单选按钮列表:

<div class="list">
    <input type="radio" name="b1" value="1">
    <input type="radio" name="b2" value="2" checked="checked">
    <input type="radio" name="b3" value="3">
</div>

我可以用这个选择器找到哪个选择了:

$('.list input[type="radio"]:checked:first').val();

即使没有选择任何元素,我仍然不会得到未定义的错误。因此,在获取元素的值之前,不需要编写额外的if语句。

下面是一个非常基本的jsfiddle示例。