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


当前回答

我知道我这么晚才加入。但值得一提的是,它需要

<label class="custom-control-label" for="myRadioBtnName">Personal Radio Button</label>

然后我在js中检查了这个。可以是这样

$(document).ready(function () { 

$("#MyRadioBtnOuterDiv").click(function(){
    var radioValue = $("input[name=myRadioButtonNameAttr]:checked").val();
    if(radioValue === "myRadioBtnName"){
        $('#showMyRadioArea').show();
    }else if(radioValue === "yourRadioBtnName"){
        $('#showYourRadioArea').show();
    }
});
});

`

其他回答

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

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

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());
        });
    });

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

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

我知道我这么晚才加入。但值得一提的是,它需要

<label class="custom-control-label" for="myRadioBtnName">Personal Radio Button</label>

然后我在js中检查了这个。可以是这样

$(document).ready(function () { 

$("#MyRadioBtnOuterDiv").click(function(){
    var radioValue = $("input[name=myRadioButtonNameAttr]:checked").val();
    if(radioValue === "myRadioBtnName"){
        $('#showMyRadioArea').show();
    }else if(radioValue === "yourRadioBtnName"){
        $('#showYourRadioArea').show();
    }
});
});

`

只看名字

$(function () {
    $('input[name="EventType"]:radio').change(function () {
        alert($("input[name='EventType']:checked").val());
    });
});
$("#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(){…});美元(文档)时的功能 (){...});).