我正在使用在线产品,由于某种原因,当它生成有问题的页面时,单选按钮集的名称是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);
关于javascript - 获取 Jquery 中选定单选按钮的 ID "name has []",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23107746/