简体   繁体   English

获取与使用Val()选择的单选按钮相同的值

[英]Getting the same value of the radio button selected using Val()

I have a form and I wish to get the value of a radio button but it always gives me the same value ("floating") even though I select a different radio button and I run this function?! 我有一个表单,希望获得单选按钮的值,但是即使我选择其他单选按钮并运行此功能,它也总是给我相同的值(“浮动”)!

HTML: HTML:

<input type="radio" id="license_floating" name="license" value="floating" class="realtime" />
<input type="radio" id="license_fixed" name="license" value="fixed" class="realtime" />

JS/JQuery: JS / JQuery:

alert($('form#quick input[name=license]').val());

The form is named quick, however, there are other forms on the page which have use the same IDs. 表单被命名为快速表单,但是页面上还有其他表单具有相同的ID。 But this shouldn't be a problem as I am referencing it correctly by referring to the form as well as the element ID. 但这不是问题,因为我通过引用表单以及元素ID正确地引用了它。

Should be asking for the selected one somehow? 应该以某种方式要求选择一个吗?

Really need help on this! 确实需要帮助!

Thanks all 谢谢大家

You can get the selected radio button with :checked 您可以使用:checked来选择单选按钮

alert($('form#quick input[name=license]:checked').val());

This can also work if you want 如果需要,这也可以工作

alert($('form#quick input.realtime:checked').val());

But you state that #quick is in other places as well, as Gumbo pointed out in the comments, ID's should be unique. 但是您要指出#quick也位于其他位置,正如Gumbo在评论中指出的那样,ID应该是唯一的。

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

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