我需要获取所选的单选按钮值。单选按钮名称为is_external
的test.html
<div class="exter_inter">
External <input type="radio" name="is_external" value="1" <?php if(isset($_GET['status']) && $_GET['status']=='1') echo "checked";?> />
Internal <input type="radio" name="is_external" value="0" <?php if(isset($_GET['status']) && $_GET['status']=='0') echo "checked";?> />
</div>
的script.js
$(document).ready(function() {
$('input:radio').change(function() {
var is_external = $("input[name='radio']:checked").val();
alert("selected values: " + is_external); //Return undefined
});
} );
答案 0 :(得分:3)
$(document).ready(function() {
$('input:radio').change(function() {
var is_external = $("input[name='is_external']:checked").val();//name is is_external not radio
alert("selected values: " + is_external); //Return undefined
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="exter_inter">
External
<input type="radio" name="is_external" value="1" />Internal
<input type="radio" name="is_external" value="0" />
</div>
名称为is_external
而不是radio
答案 1 :(得分:0)
<input type="radio" name="user" value="newUser">New User
<input type="radio" name="user" value="registeredUser">Registered User
$(document).ready(function () {
$("input:radio").change(function () {
var x = $("input[name='user']:checked").val();
console.log(x);
});
});