简体   繁体   中英

Jquery how to get list of checked radio button value?

I have this jquery script

  function radiolist() {
        var radioChecked = [];
        var radios = $("input[type='radio']");
        //alert(radios.length);
        
        for (var i = 0; i < radios.length; i++) {
            if (radios[i].checked) {
                radioChecked.push(radios[i].val());
            }
        }
        alert(radioChecked);
    }

I want to save the list of checked radio buttons in the radioChecked array and access them. But I get the following error

Uncaught TypeError: radios[i].val is not a function

Why can`t to get radio button value ?

When you index an element in jQuery, it doesn't rewrap the element in jQuery. So, you can either use the vanilla javascript approach with radios[i].value , or rewrap it in jQuery with $(radios[i]).val() .

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