开发者

jQuery Getting Wrong Value from Checkbox Group?

Given the follow开发者_如何学Pythoning code, jQuery seems to only alert "1" as the value even after the user has selected radiobutton 2. What am I missing here? Shouldn't the trailing jQuery alert "2" when the second radiobutton is selected?

<p>
  Coal  <input type="radio" name="example" value="1" checked /> 
  Candy <input type="radio" name="example" value="2" />
</p>

I'm using this code in firebug to test:

r = $(":radio[name='example']").val();
alert(r);


$(":radio[name='example']") gives you all radios with name=example .val() then takes the value of the first of those. you want $(":radio[name='example']:checked").val()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜