gpt4 book ai didi

javascript - Jquery 删除 NextAll 不适用于选择

转载 作者:行者123 更新时间:2023-12-02 15:42:32 26 4
gpt4 key购买 nike

这是我的问题

在 StackOverflow 上找到的相关/类似问题中找不到任何可行的解决方案。

我有一个充满ajax数据的选择,我们称其为“mainSelect”,它有一个“selectSomething”类。在事件(change/keyup)上,如果所选值不是 '',那么它会自动在第一个选择下添加一个“subChoice”选择,该选择也具有“selectSomething”类。

当用户在第二个选择中选择某些内容时,它会在当前选择下添加(或不添加,取决于选择的值)一个新选择(仍具有 selectSomething 类)。等等。像这样:

这是 html 部分:

<div class='choiceDiv'>
<select class='selectSomething' id='masterSelect'>
</select>
</div>

这是 Js 部分:

    $(document).ready(function(){
populateMainSelect();
//does its job correctly
});
$(document).on('change keyup','.selectSomething',function(){
if($(this).val()!=''){
//Need here to remove all sub-choices selects
//NEXT LINE NOT WORKING !
$(this).parent().nextAll('.selectSomething').remove();
//And add or not a new one
if(/*NeedToAddASubSelectCheck*/){
//PopulateMySelect is a function that fills a jquery element. Like this :
// function PopulateMySelect(elementToFill,dataToEnter,callback);
PopulateMySelect($('<select>',{
class:'selectSomething'
}).appendTo('#choiceDiv')),data,function(){
console.log('select is created, filled and added on page');
});
}
}
});

也许这个“逻辑”在某种程度上不是最佳的,你可能会给我一些建议,

但现在对我来说最重要的是这一行

$(this).parent().nextAll('.selectSomething').remove();

不工作...有什么想法吗?

感谢您的阅读/帮助

编辑:

如果我在控制台中输出:

console.log($(this).nextAll('.selectSomething'));

我得到一个长度为0的对象[]...

最佳答案

您需要使用

$(this).nextAll('.selectSomething').remove();

而不是

$(this).parent().nextAll('.selectSomething').remove();

因为您需要获取最近的父级,然后找到其中的任何选择并将其删除。

说明:

您的代码

$(this)         // this here is the selectSomething dropdown
.parent() // this is the choiceDiv parent div element
.nextAll('.selectSomething') // there nothing next to choiceDiv
.remove(); // that's why this remove does not works here

关于javascript - Jquery 删除 NextAll 不适用于选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32459103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com