当在 JQuery 中更改选择时,我试图显示我的单选按钮值。但是,无论我为单选按钮分配什么值,它在我的警告框中只显示 0。我已经尝试了很多东西,但没有得到任何运气。
这是我的 Jquery 代码:
<script type="text/javascript">
$(function() {
$("input[name='sec_officer']").change(function(){
if ($("input[@name='sec_officer']:checked").val() == '5'){
alert($("input[@name='sec_officer']:checked").val());
} else
{alert($("input[@name='sec_officer']:checked").val()); }
});
});
</script>
这是我的 HTML 代码段:
<tr>
<td>Security Officer oversight:</td>
<td><input type="radio" name="sec_officer" id="sec_officer_no" value="5" checked="checked"/>
No <input type="radio" name="sec_officer" id="sec_officer_yes" value="6" />
Yes</td>
</tr>
提前致谢!!!
最佳答案
选定的元素将以此形式返回。所以你可以这样做。
$("input[name='sec_officer']").change(function(){
console.log($(this).val()); // Checked value
})
编辑: Jsbin
$("input[name='sec_officer']").change(function(){
var value=$(this).val();
if(value==5) console.log("You selected 5");
else console.log("You selected " + value);
});
关于jquery - 即使将其更改为其他值(使用 JQuery),单选按钮值仍显示 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17983885/