Javascript nextAll().remove()不处理jquery中的动态添加元素
在我的代码中,我使用jquery从数据库加载具有n级性能的数据。 但是当我更改父下拉列表时,所有下一个元素都不会删除,请帮助。 先谢谢你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
$(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已经要求尊重你的回答,但是问题解决了。。