[英]Control javascript hide/show div with radio controls without jquery
[英]jQuery hide/show div, with radio button control
我有几个同名的单选按钮。
Office <input type="radio" name="owner_type" value="0" />
Agent <input type="radio" name="owner_type" value="1" />
并拥有jQuery代码的一部分,它必须显示或隐藏部分代码。
$('[name="owner_type"]').change(function(){
if ($('[name="owner_type"]').val() == 1) {
alert("it's 1 now");
$('#agents').show();
} else {
alert("it's 0 now");
$('#agents').hide();
}
});
添加了警报以进行调试。 它始终是关于0的警报。无论现在检查哪个无线电。 我希望,你可以帮助我。 谢谢
你可以这样做:
$('[name="owner_type"]').change(function () {
if ($('[name="owner_type"]:checked').val() === "1") {
alert("it's 1 now");
$('#agents').show();
} else {
alert("it's 0 now");
$('#agents').hide();
}
});
实际上,你这样做:
$('[name="owner_type"]').val()
它总是会获取第一个单选按钮的值,无论是否已选中。
为了获得change
事件上已检查单选按钮的值,您可以使用:checked
选择器,如上所示。
var $oTypes = $('input[name="owner_type"]').change(function () {
//$('#agents').toggle($oTypes.filter(':checked').val() == 1);
$('#agents').toggle(this.value == 1);
});
尝试这样的事情
$('input[name="owner_type"]').change(function(){
if (this.value == 1) {
alert("it's 1 now");
$('#agents').show();
} else {
alert("it's 0 now");
$('#agents').hide();
}
});
试试这个,使用a :checked
选择器
$('[name="owner_type"]').change(function(){
if($('[name="owner_type"]:checked').val() == 1) {
alert("it's 1 now");
$('#agents').show();
} else {
alert("it's 0 now");
$('#agents').hide();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.