get value from radio group using jquery

半城伤御伤魂 提交于 2019-12-17 15:43:21

问题


I am trying to get value of radio group with name managerelradio. My html code for this radio group is.

 <label><input type="radio" name="managerelradio" value="Yes" id="Add">Add</label>
 <label><input type="radio" name="managerelradio" value="No" id="Remove">Remove</label>

and Jquery for this is..

    var manageradiorel = $('input[name = "managerelradio"]:checked' , '#managechildform').val();
 alert(manageradiorel);

its showing me undefined.

Though I have also tried it as.

 var manageradiorel = $('input[name = "managerelradio"]:checked').val();
 alert(manageradiorel);

But still I am getting undefined value.


回答1:


Try this

var manageradiorel = $("input:radio[name ='managerelradio']:checked").val();
alert(manageradiorel);

Plese check this DEMO ..it will work fine

Note: One of your radio button must be selected. Otherwise it will return undefined

You can use checked attribute to make a radio button selected as default




回答2:


It works for me

$('input[name="managerelradio"]').on('change', function(e) {

    var manageradiorel = e.target.value;
    alert(manageradiorel);

});

Exaple here




回答3:


A small jQuery extension to make this a little easier:

jQuery.fn.extend({
    groupVal: function() {
        return $(this).filter(':checked').val();
    }
});

// Usage:
$("input[name='managerelradio']").groupVal();

// Or even:
$("[name='managerelradio']").groupVal();


来源:https://stackoverflow.com/questions/7723505/get-value-from-radio-group-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!