JQuery:获取单选按钮值 [英] JQuery: get radio button value
问题描述
我有以下HTML:
HTML:
<input type="radio" name="abc" value="0" selected="selected" style="display:none" />
<input type="radio" name="abc" value="1" />1+
<input type="radio" name="abc" value="2" />2+
<input type="radio" name="abc" value="3" />3+
获取所选单选按钮的JQuery
JQuery to get the selected radio button
$('input:radio[name=abc]:checked').val();
为什么上述代码在页面加载时不起作用,在用户选择单选按钮之前?这很奇怪,因为上面的代码在用户选择单选按钮后才有效。
Why doesn't the code above work on page load, BEFORE a user selected a radio button? It's strange because the code above does work AFTER a user selected a radio button.
在我看来,我已将默认的单选按钮值设置为 0
,但是如果你
It seems to me that I have set the default radio button value to be 0
, but if you
意思是,如果选中单选按钮值,则返回所选值 - 否则返回0(当没有选择任何值)
Meaning, if the radio button value is selected, return the selected value - otherwise, return 0 (when no value has been selected)
推荐答案
您使用的是错误的属性。对于单选按钮,您必须使用选中的
属性,而不是选中的
属性。
You are using the wrong attribute. For radio buttons you have to use the checked
attribute and not the selected
attribute.
<input type="radio" checked="checked">
或只是:
<input type="radio" checked>
这篇关于JQuery:获取单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!