当另一个选择改变时jquery为空选择
您好,我正在尝试使用以下代码清空输入选择:
$(document).on('change', '#firstselect', function(){
if($('#secondselect').length > 1){
$('#secondselect').empty();
var html = '<option value="9997" selected="selected">ESCOJE UNA SUBCATEGORIA</option>'
$('#subcategoria').html(html);
}
});
现在它可以正常使用 0 但为什么如果它是 1 则不能呢?
注意:首先,选择具有显示的选项,该选项在清空后添加,当第一个选择更改时,它会填充来自服务器的信息,但我没有放置该代码,因为它不相关。
编辑:看起来如果你确实更改为 >= 它确实有效,但如果它 >=2 则不起作用,为什么?
Hi I'm trying to empty an input select with this code:
$(document).on('change', '#firstselect', function(){
if($('#secondselect').length > 1){
$('#secondselect').empty();
var html = '<option value="9997" selected="selected">ESCOJE UNA SUBCATEGORIA</option>'
$('#subcategoria').html(html);
}
});
Now it works fine with 0 but why if its 1 it does not?
Note: At first the select has the option that is shown which is added after is emptied and when the first select is changued it is filled with info from a server but I did not put that code since it's not relevant.
EDIT: It looks that if you do change to >= it does work but if it's >=2 does not why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在比较
select
元素的长度,而不是其中的option
元素的数量。因此只有 1 个选择元素与该 ID 匹配。You are comparing the length of the
select
element and not the number ofoption
elements inside it. So there's only ever 1 select element that matches that ID.