admin管理员组文章数量:1391925
I have these radio buttons.
<input type="radio" id="flip" name="flip" value="Head" checked="checked" /> Head
<input type="radio" id="flip" name="flip" value="Tail" /> Tail
I am trying to process a form with ajax for which I tried to get the value of these radio buttons with this
var dataString = {
flip: $("#flip").val()
};
console.log(dataString.flip);
Now when I do console.log
to check the value its sending I am always getting Head
and not Tail even if I select Tail as a choice. Can anyone help me as why is it so?
I have these radio buttons.
<input type="radio" id="flip" name="flip" value="Head" checked="checked" /> Head
<input type="radio" id="flip" name="flip" value="Tail" /> Tail
I am trying to process a form with ajax for which I tried to get the value of these radio buttons with this
var dataString = {
flip: $("#flip").val()
};
console.log(dataString.flip);
Now when I do console.log
to check the value its sending I am always getting Head
and not Tail even if I select Tail as a choice. Can anyone help me as why is it so?
- 2 you cannot have the same ID multiple times! – Luke Commented Apr 3, 2017 at 15:30
-
id="flip"
=>class="flip"
--$("#flip")
=>$(".flip")
– Funk Forty Niner Commented Apr 3, 2017 at 15:30 - 1 ID's Must Be Unique, specifically because it will cause problems in JavaScript and CSS when you try to interact with those elements. – Jay Blanchard Commented Apr 3, 2017 at 15:35
4 Answers
Reset to default 3get value by name not id :
$("input[name='flip']:checked").val();
id
cannot be duplicate. It need to be unique.In this case you can use name
attribute
Use name to select get the selected radio button value
$("input:radio[name=flip]:checked").val();
you cannot have the same ID multiple times! that is the reaon why it only displays one of the two.
ID #flip
is an unique element(you cannot have more than more #flip
element). Use .flip
instead of #flip
.
And also change id="flip"
to class="flip"
本文标签: javascriptGet Radio Button value with jqueryAjaxStack Overflow
版权声明:本文标题:javascript - Get Radio Button value with jqueryAjax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744703326a2620691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论