我有这个代码
$("#tag_price").on('change', function(event) {
if ($(this).val() == -2)
$('<input type="text" class="ipt" id="customprice" name="customPrice" />').insertAfter(this),
$('<p id="customprice" class="semi_margin_top">Introduce el precio exacto</p>').insertAfter(this);
else if ($(this).val() !== -2)
$('#customprice').hide;
});
第一部分正在工作,当输入值为-2时,显示第二个输入。但我希望它在选择另一个值时消失,因为如果没有,第二个输入将永远保留。
我希望第二个输入仅在选择值 -2 时显示,并在选择另一个值时消失。我以为我可以用 Jquery 和 hide 来实现这一点,但我不会工作。
谢谢!
最佳答案
hide()
的推论您正在寻找的是 show()
。您的代码还需要注意一些其他问题。
首先,您的 if
周围缺少 { }声明,所以你的else if
应该抛出一个错误 $('<p...')
该行位于 if 之外。
其次,不需要 else if
因为没有第三个选择。该值要么是“-2”,要么不是。
现在,每次有人选择“-2”项时,您都会附加一个新元素,这不是正确的方法。检查这些元素是否已经在 DOM 中,如果不存在则添加它们。如果是,请调用 show()
.
if ($(this).val() === -2) {
$('#customprice').show();
}
关于javascript - 使用 Jquery 隐藏所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24168511/