简体   繁体   中英

Get selected radio button value in javascript

I have dynamically created radio buttons and have assigned them ids. Now when i try,

function updateAO(id2) {
    var status = $('input[name=id2]:checked').val();
    alert(status);
}

It prints undefined. Can anyone please tell how to get this value. Thanks in advance!!

您没有正确使用参数。

var status = $('input[name="' + id2 + '"]:checked').val();

The :checked selector will only select inputs currently checked, if the passed input isnt checked at the time, status will remain unset, try changing your JS to:

function updateAO(id2) {
    var status = $('input[name='+id2+']').val();
    alert(status);
}

To get the selected radio button value use

function updateAO(id2) {
    var status = $('input[name=id2]:checked').val();
    alert(status);
}

To select normal radio button use

function updateAO(id2) {
    var status = $('input[name=id2]').val();
    alert(status);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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