Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript nextAll().remove()不处理jquery中的动态添加元素_Javascript_Jquery_Html_Dom - Fatal编程技术网

Javascript nextAll().remove()不处理jquery中的动态添加元素

Javascript nextAll().remove()不处理jquery中的动态添加元素,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,在我的代码中,我使用jquery从数据库加载具有n级性能的数据。 但是当我更改父下拉列表时,所有下一个元素都不会删除,请帮助。 先谢谢你 $(document).ready(function() { $(document).on('change', '#subcat', function() { var id = $(this).val(); $(this).nextAll('.remove').remove(); var options

在我的代码中,我使用jquery从数据库加载具有n级性能的数据。 但是当我更改父下拉列表时,所有下一个元素都不会删除,请帮助。 先谢谢你

$(document).ready(function() {
    $(document).on('change', '#subcat', function() {
        var id = $(this).val();
        $(this).nextAll('.remove').remove();
        var options = "<option value=0>--select subcategory--</option>";
        $.ajax({
            type: 'get',
            url: '/category/' + id + '/dropDownDynamic',
            success: function(responseText) {
                if (responseText != "") {
                    $.each(responseText, function(index, value) {
                        options += '<option value=' + index + '>' + value + '</option>'
                    });
                    $('#subcatCustom').append('<div class="form-group marginRemove remove"><label class="col-sm-2 control-label">Select SubCategory:</label><div class="controls col-sm-5"><select name="parent" id="subcat" class="form-control">' + options + '</select></div></div>');
                }
            },
        });
    });
});

因为它们不是当前
subcat
元素的同级,所以它们是当前
元素的同级。请删除
,然后重试

$(this).closest('.remove').nextAll('.remove').remove();
在事件处理程序中,this
引用
#subcat
元素,它是
的后代。remove
元素,因此我们使用它来查找
。remove
this
的祖先,然后查找该祖先的下一个同级


注意:元素的ID必须是唯一的,因此使用
subcat
作为类而不是ID。

因为它们不是当前
subcat
元素的同级,它们是当前
的同级。请删除
,然后重试

$(this).closest('.remove').nextAll('.remove').remove();
在事件处理程序中,this
引用
#subcat
元素,它是
的后代。remove
元素,因此我们使用它来查找
。remove
this
的祖先,然后查找该祖先的下一个同级


注意:元素的ID必须是唯一的,因此,使用
subcat
作为类而不是ID。

在问题中添加
HTML
,请发布您的HTMLalso@SandeeproopTushar已经要求您提供这方面的回答,但问题已经解决。另外,在问题中添加
HTML
,请发布您的HTMLalso@SandeeproopTushar已经要求尊重你的回答,但是问题解决了。。