简体   繁体   English

获取Jquery中“名称具有[]”的选定单选按钮的ID

[英]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 []" 我正在使用在线产品,由于某种原因,当它生成有问题的页面时,单选按钮集的名称为product_id_page-0 [] <-“注意[]”

What I want to do is get the ID of the selected radio button using Jquery. 我想要做的是使用Jquery获取所选单选按钮的ID。 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: 如果这是名称而不是id,请使用:

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

see more info here 在这里查看更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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