【发布时间】:2021-04-21 06:20:32
【问题描述】:
我有两个具有拖放功能的数据表。每行数据表都包含 ID。当我将表 1 的行拖到表 2 中时。存储到数组中的数据。我有用于将 ID 推送到数组中的 addArray 函数,还可以过滤重复的 ID 并使其成为唯一数组。现在我想创建包含 iD 作为值的选择选项元素。我希望在拖放时重新创建选择选项。如果有任何帮助,我将不胜感激。
JS
$(document).ready(function () {
new Sortable(drag, {
group: 'sortables',
onChange: function (e, tr) {
addArray();
},
});
new Sortable(drop, {
group: "sortables",
onChange: function (event, tr) {
addArray();
},
});
function addArray() {
let articles = [];
$('#drop').children().each(function () {
articles.push($(this).data('pk'))
});
let uniqueArray = articles.filter((item, index, array) => { // filter Duplicate ID
return array.indexOf(item) === index
})
$.each(uniqueArray, function (index, value) { // Create Option Element using Each ID form Array
$('#id_articles').append($('<option/>', { value: value, selected: '' }));
});
console.log(uniqueArray)
};
});
输出
<select name="articles" id="id_articles" multiple="" style="display: none;">
<option value="67" selected="selected"></option>
<option value="66" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="66" selected="selected"></option>
<option value="66" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="67" selected="selected"></option>
<option value="66" selected="selected"></option>
</select>
【问题讨论】:
-
console.log(uniqueArray);的输出是什么 -
输出是过滤器 ["67", "66"] 作为我想要做的,但在 html 选择选项包含重复值
标签: javascript html jquery bootstrap-4