jquery获取select项并移动到另一个select里
warning:
这篇文章距离上次修改已过457天,其中的内容可能已经有所变动。
要使用jQuery将选中的<option>从一个<select>元素移动到另一个<select>元素,你可以使用以下代码示例:
HTML部分:
<select id="sourceSelect">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
<button id="moveRight">-></button>
<button id="moveLeft"><-</button>
<select id="destinationSelect" multiple="multiple">
<!-- 这里可以预先放一些<option> -->
</select>jQuery部分:
$(document).ready(function() {
$('#moveRight').click(function() {
$('#sourceSelect option:selected').appendTo('#destinationSelect');
});
$('#moveLeft').click(function() {
$('#destinationSelect option:selected').appendTo('#sourceSelect');
});
});这段代码中,我们定义了三个元素:两个<select>和一个按钮。通过点击按钮,可以将选中的选项从一个<select>移动到另一个。#moveRight按钮将选中的选项移动到右边的<select>,而#moveLeft按钮则将选中的选项从右边的<select>移回左边的<select>。
评论已关闭