简体   繁体   中英

Get ID of selected radio button in Jquery “name has []”

I am using an online product and for some reason when it generated the page in question the name of the radio button set is product_id_page-0[] <-- "Note the []"

What I want to do is get the ID of the selected radio button using Jquery. If I use

    var userType =$('#product_id_page-0[]:radio:checked').attr('id');
    alert("xmlvalue is: " + userType);

Nothing happens

If I use

    var userType =$('#product_id_page-0:radio:checked').attr('id');
    alert("xmlvalue is: " + userType);

I get an alert with Undefined.

I can not change the radio button name group, but I am thinking that the brackets might be breaking something here.

Please advice.

You need to scape the meta character with the two slashes:

var userType =$('#product_id_page-0\\[\\]:radio:checked').attr('id');
    alert("xmlvalue is: " + userType);

if this is the name instead of id use:

var userType =$('[name=product_id_page-0\\[\\]]:radio:checked').attr('id');
        alert("xmlvalue is: " + userType);

see more info here

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