gpt4 book ai didi

javascript - 交换从选择 1 到选择 2 的所有选项

转载 作者:太空宇宙 更新时间:2023-11-04 16:11:06 25 4
gpt4 key购买 nike

我想在有人点击交换按钮后立即更改列表的值,我的代码链接是 http://codepen.io/varadekd/pen/OMOYpa

HTML

<center>
<table>
<tr>
<th>list 1 </th>
<th> list 2 </th>
</tr>
<tr>
<td>
<select id="mySelect1" size="5" multiple style="width:200px">
<option value="opt1">First item of list 1</option>
<option value="opt2">Second item of list 1</option>
<option value="opt3">Thrid item of list 1</option>
<option value="opt4">Fourth item of list 1</option>
<option value="opt5">Fifth item of list 1</option>
</select>
</td>

<td>
<select id="mySelect2" size="5" multiple style="width:200px">
<option value="opt6">First item of list 2</option>
<option value="opt7">Second item of list 2</option>
<option value="opt8">Thrid item of list 2</option>
<option value="opt9">Fourth item of list 2</option>
<option value="opt10">Fifth item of list 2</option>
</select>
</td>
</tr>
</table>
</center>
<center>
<button id="toRight">Swap to list 1</button>

<button id="toLeft"> Swap to list 2</button> <br/>

<button id="swipe"> Swap </button>
<input type="button" onClick="count1()" value="Item in list 1" />
<input type="button" onClick="count2()" value="Item in list 2" />
</center>

JS

    $(function () { 
function moveItems(origin, dest) {
$(origin).find(':selected').appendTo(dest);
}

$('#toLeft').click(function () {
moveItems('#mySelect1', '#mySelect2');
});

$('#toRight').on('click', function () {
moveItems('#mySelect2', '#mySelect1');
});
});

function count1(){
var store = $('select#mySelect1 option').length;
alert(store);
}

function count2(){
var store2 = $('select#mySelect2 option').length;
alert(store2);
}

我必须将所有选项从列表 1 移动到列表 2,同时将所有选项从列表 2 移动到列表 1

最佳答案

一种简单的方法是使用以下 JQuery:

$('#swipe').click(function () {

var selOne = $("#mySelect1").html();
var selTwo = $("#mySelect2").html();

$("#mySelect1").html(selTwo);
$("#mySelect2").html(selOne);

});

Working CodePen

关于javascript - 交换从选择 1 到选择 2 的所有选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34891796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com