admin管理员组文章数量:1135133
Here is my code:
<table>
<tr>
<td>Sales Promotion</td>
<td><input type="radio" name="q12_3" value="1">1</td>
<td><input type="radio" name="q12_3" value="2">2</td>
<td><input type="radio" name="q12_3" value="3">3</td>
<td><input type="radio" name="q12_3" value="4">4</td>
<td><input type="radio" name="q12_3" value="5">5</td>
</tr>
</table>
<button id="submit">submit</button>
Here is JS:
$(function(){
$("#submit").click(function(){
alert($('input[name=q12_3]').val());
});
});
Here is JSFIDDLE! Every time I click button it returns 1. Why? Can anyone help me?
Here is my code:
<table>
<tr>
<td>Sales Promotion</td>
<td><input type="radio" name="q12_3" value="1">1</td>
<td><input type="radio" name="q12_3" value="2">2</td>
<td><input type="radio" name="q12_3" value="3">3</td>
<td><input type="radio" name="q12_3" value="4">4</td>
<td><input type="radio" name="q12_3" value="5">5</td>
</tr>
</table>
<button id="submit">submit</button>
Here is JS:
$(function(){
$("#submit").click(function(){
alert($('input[name=q12_3]').val());
});
});
Here is JSFIDDLE! Every time I click button it returns 1. Why? Can anyone help me?
Share Improve this question edited Jun 19, 2015 at 16:42 Beetle 1,99916 silver badges33 bronze badges asked Aug 4, 2013 at 13:29 SPGSPG 6,19714 gold badges50 silver badges81 bronze badges 07 Answers
Reset to default 229In your code, jQuery just looks for the first instance of an input with name q12_3
, which in this case has a value of 1
. You want an input with name q12_3
that is :checked
.
$("#submit").click(() => {
const val = $('input[name=q12_3]:checked').val();
alert(val);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>Sales Promotion</td>
<td><input type="radio" name="q12_3" value="1">1</td>
<td><input type="radio" name="q12_3" value="2">2</td>
<td><input type="radio" name="q12_3" value="3">3</td>
<td><input type="radio" name="q12_3" value="4">4</td>
<td><input type="radio" name="q12_3" value="5">5</td>
</tr>
</table>
<button id="submit">submit</button>
Note that the above code is not the same as using .is(":checked")
. jQuery's is()
function returns a boolean (true or false) and not (an) element(s).
Because this answer keeps getting a lot of attention, I'll also include a vanilla JavaScript snippet.
document.querySelector("#submit").addEventListener("click", () => {
const val = document.querySelector("input[name=q12_3]:checked").value;
alert(val);
});
<table>
<tr>
<td>Sales Promotion</td>
<td><input type="radio" name="q12_3" value="1">1</td>
<td><input type="radio" name="q12_3" value="2">2</td>
<td><input type="radio" name="q12_3" value="3">3</td>
<td><input type="radio" name="q12_3" value="4">4</td>
<td><input type="radio" name="q12_3" value="5">5</td>
</tr>
</table>
<button id="submit">submit</button>
in your selector, you should also specify that you want the checked radiobutton:
$(function(){
$("#submit").click(function(){
alert($('input[name=q12_3]:checked').val());
});
});
You might want to change selector:
$('input[name=q12_3]:checked').val()
There is another way also. Try below code
$(document).ready(function(){
$("input[name='gender']").on("click", function() {
alert($(this).val());
});
});
$('input:radio[name=q12_3]:checked').val();
DEMO : https://jsfiddle.net/ipsjolly/xygr065w/
$(function(){
$("#submit").click(function(){
alert($('input:radio:checked').val());
});
});
use this script
$('input[name=q12_3]').is(":checked");
本文标签:
版权声明:本文标题:javascript - In jQuery, how do I get the value of a radio button when they all have the same name? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736816960a1954128.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论