我试图将select1中的选定选项移动到按钮的click
上的select2。这是我的HTML代码:
<p>
<select id="select1" size="10" style="width: 25%" multiple>
<option value="purple">Purple</option>
<option value="black">Black</option>
<option value="orange">Orange</option>
<option value="pink">Pink</option>
<option value="grey">Grey</option>
</select>
</p>
<button type="button" click.delegate="trig()">Add</button>
<p>
<select id="select2" size="10" style="width: 25%" multiple>
<option value="white">White</option>
<option value="red">Red</option>
<option value="yellow">Yellow</option>
<option value="blue">Blue</option>
<option value="green">Green</option>
</select>
</p>
这是我的JS代码包含按钮
export class App {
constructor() {
}
trig() {
}
}
我需要在trig()
中放置什么,以便点击按钮将所选项目移动到另一个列表?
你可以循环通过selectedColors1
并获得每个所选项目的index
。然后将它们推送到color2
阵列并逐个从colors
阵列中删除它们。
但是:Kua zxsw指出
CodeSandbox
HTML:
export class App {
colors1 = [
{ id: "purple", name: "Purple" },
{ id: "black", name: "Black" },
{ id: "orange", name: "Orange" }
];
colors2 = [
{ id: "white", name: "White" },
{ id: "red", name: "Red" },
{ id: "blue", name: "Blue" }
];
selectedColors1 = [];
selectedColors2 = [];
add() {
this.selectedColors1.forEach(selected => {
// get the index of selected item
const index = this.colors1.findIndex(c => c.id === selected);
this.colors2.push(this.colors1[index]); // add the object to colors2
this.colors1.splice(index, 1); // remove from colors1
});
}
}
我根据你的使用情况添加了<select multiple value.bind="selectedColors1">
<option repeat.for="color of colors1" model.bind="color.id">
${color.name}
</option>
</select>
<button type="button" click.delegate="add()">Add</button> <br />
<select multiple value.bind="selectedColors2">
<option repeat.for="color of colors2" model.bind="color.id">
${color.name}
</option>
</select>
按钮。请参阅以下链接,我已更新解决方案。我想你的想法有点相同。
add
如果您需要更多帮助,请对此发表评论。