简体   繁体   English

jQuery,获取选中的单选按钮的值,为什么这不起作用?

[英]JQuery, getting value of checked radio button, why is this not working?

I am trying like below to get the value of a checked radio button but it just keeps giving undefined. 我正在尝试像下面这样来获取选中的单选按钮的值,但是它一直在给undefined。 I am using Firefox on Ubuntu so I don't know if its some weird quirk of the browser or what, I would appreciate any advice as this is driving me crazy: 我在Ubuntu上使用Firefox,所以我不知道它是否是浏览器的怪异怪癖,还是什么,我希望您提出任何建议,因为这会使我发疯:

            <input name="tagRow_ddd" type="radio" value="p">
                <input name="tagRow_ddd" type="radio" value="r">


alert($('input[name=tagRow_ddd]:checked').val())

Jfiddle: http://jsfiddle.net/fD7fP/6/ Jfiddle: http : //jsfiddle.net/fD7fP/6/

that's exactly the output it should give. 这正是它应该提供的输出。 the :checked selector only finds checked elements... you haven't any. :checked选择器仅查找选中的元素...您还没有。

http://jsfiddle.net/fD7fP/7/ http://jsfiddle.net/fD7fP/7/

Because by default nothing is checked. 因为默认情况下不检查任何内容。 If you have either checked by default it works. 如果您默认选中其中任何一个,则可以使用。

Check working example at http://jsfiddle.net/fD7fP/8/ http://jsfiddle.net/fD7fP/8/中查看工作示例

You need to quote the attribute value http://api.jquery.com/attribute-equals-selector/ 您需要引用属性值http://api.jquery.com/attribute-equals-selector/

So it should be 所以应该

alert($("input[name='tagRow_ddd']:checked").val())

Try making one selected by default. 尝试默认选择一个。 Lke so 所以

<input name="tagRow_ddd" type="radio" value="p">
<input name="tagRow_ddd" type="radio" value="r">

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

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