繁体   English   中英

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

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

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

我想要做的是使用Jquery获取所选单选按钮的ID。 如果我用

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

什么都没发生

如果我用

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

我收到未定义的警报。

我无法更改单选按钮名称组,但是我认为方括号可能会破坏此处的内容。

请指教。

您需要使用两个斜杠对元字符进行转义:

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

如果这是名称而不是id,请使用:

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

在这里查看更多信息

暂无
暂无

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

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