【发布时间】:2020-10-10 22:47:57
【问题描述】:
决定开始学习jquery来增强我的html+css。 我一直在纠结下面的语句,我试图根据一个人在单选按钮上的选择来隐藏/显示输入字段(单选按钮称为 oneFacesLabel、twoFacesLabel 和 threeFacesLabel)。
在开始时有效,但是一旦显示所有 3 个选项,如果我单击第一个选项,它就不会隐藏其他 2 个。我知道为什么它不会触发,我尝试在每个 if 隐藏之后放置 else选项,但如果之后不能。我有什么选择?
编辑:解释一下,如果用户选择 oneFaceLabel 则 image1 显示,如果他们选择 twofacelabel,则 image1 和 image2 显示,如果他们返回 onefacelabel 则 image2 应该消失
$(document).ready(function() {
$('input[type="radio"]').click(function() {
if ($(this).attr('id') == 'oneFaceLabel') {
$('#image1').show();
} else if ($(this).attr('id') == 'twoFacesLabel') {
$('#image1').show();
$('#image2').show();
} else if ($(this).attr('id') == 'threeFacesLabel') {
$('#image1').show();
$('#image2').show();
$('#image3').show();
} else {
$('#image1').hide();
$('#image2').hide();
$('#image3').hide();
}
});
});
【问题讨论】:
-
嗨,您可以先隐藏所有图像,即:
$('.someimageclass').hide();将其放在if语句之前并查看一次。 -
@Swati 嘿,图片一开始就被隐藏了。如果用户选择 oneFaceLabel 然后 image1 显示,我试图完成什么,如果他们选择 twofacelabel,然后 image1 和 image2 显示,如果他们回到 onefacelabel 那么 image2 应该消失
标签: jquery