在我的代码中,我使用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>');
}
},
});
});
});
此行不起作用。
$(this).nextAll('.remove').remove();
答案 0 :(得分:0)
因为他们不是当前subcat
元素的兄弟,所以他们是当前.remove
的兄弟姐妹,所以试试
$(this).closest('.remove').nextAll('.remove').remove();
在您的事件处理程序this
中引用#subcat
元素,它是.remove
元素的后代,因此我们使用.closest()来查找.remove
this
的祖先,然后找到祖先的下一个兄弟姐妹
注意:元素的ID必须是唯一的,因此请使用subcat
作为类而不是ID。