我有一个jQuery代码,它将所有属性从面板的一侧带到另一侧。此按钮的作用相当于“全部添加”。下面的代码实现了这一点。
到目前为止,我已经做了一些修改,以满足我的目的。我尝试的以下代码仍然可以作为"Add All“
它不断地将所有的值从一个表带到另一个表,而不是那些行业= auto的表。
这是原始的“全部添加”代码。
this.container.find(".add-all").click(function() {
var options = that.element.find('option').not(":selected");
if (that.availableList.children('li:hidden').length > 1) {
that.availableList.children('li').each(function(i) {
if (jQuery(this).is(":visible")) jQuery(options[i-1]).attr('selected', 'selected');
});
} else {
options.attr('selected', 'selected');
}
that._populateLists(that.element.find('option'));
return false;
});这就是我到目前为止尝试过的.
this.container.find(".add-auto").click(function() {
var options = that.element.find('option').not(":selected");
var elements = $('li').find("[industry]")
if (that.availableList.children('li:hidden').length > 1) {
that.availableList.children('li').each(function(i) {
if (jQuery(this).is(":visible") && jQuery(this).find("[industry = 'auto']")) jQuery(options[i-1]).attr('selected', 'selected');
});
} else {
that.availableList.children('li').each(function (i) {
if (jQuery(this).find("[industry = 'auto']")) jQuery(options[i-1]).attr('selected','selected');
});
}
that._populateLists(that.element.find('option'));
return false;
});发布于 2019-06-18 03:30:14
如果没有更好的环境,就很难知道什么对你不起作用。我唯一能看到的问题就是混淆你.each()中的this。
请考虑以下几点:
this.container.find(".add-auto").click(function() {
var options = that.element.find('option').not(":selected");
var elements = $('li').find("[industry]")
if (that.availableList.children('li:hidden').length > 1) {
that.availableList.children('li').each(function(i, el) {
if (jQuery(el).is(":visible") && jQuery(el).find("[industry='auto']")){
jQuery(options[i-1]).attr('selected', 'selected');
}
});
} else {
that.availableList.children('li').each(function (i, el) {
if (jQuery(el).find("[industry='auto']")){
jQuery(options[i-1]).attr('selected','selected');
}
});
}
that._populateLists(that.element.find('option'));
return false;
});如果我们传入索引(i)和元素(el),就可以确保this没有冲突。
希望这能有所帮助。
https://stackoverflow.com/questions/56622712
复制相似问题