问问题
65220 次
4 回答
103
var $options = $("#myselect > option").clone();
$('#secondSelectId').append($options);
于 2012-05-28T08:28:47.273 回答
14
<select multiple="true" class="multiselect1" name="myselecttsms1">
<option value="1" rel="0" title="One">One</option>
<option value="2" rel="1" title="Two">Two</option>
<option value="4" rel="3" title="Four">Four</option>
<option value="5" rel="4" title="Five">Five</option>
<option value="6" rel="5" title="Six">Six</option>
</select>
<select multiple="true" class="multiselect2" name="myselecttsms2" size="6">
</select>
<button class="add">Add</button>
<button class="addAll">Add All</button>
<button class="remove">Remove</button>
<button class="removeAll">Remove All</button>
jQuery:
$('.add').on('click', function() {
var options = $('select.multiselect1 option:selected').sort().clone();
$('select.multiselect2').append(options);
});
$('.addAll').on('click', function() {
var options = $('select.multiselect1 option').sort().clone();
$('select.multiselect2').append(options);
});
$('.remove').on('click', function() {
$('select.multiselect2 option:selected').remove();
});
$('.removeAll').on('click', function() {
$('select.multiselect2').empty();
});
于 2012-05-28T08:41:47.703 回答
9
有一种更简单的方法可以做到这一点:
var options = $("#myOldSelect").html();
$('#myNewSelect').html(options);
如果您想将新的 Select 添加到 DOM 并包含已复制的选项,这非常方便:
var options = $("#myOldSelect").html();
$("#domLocation").append("<select id='myNewSelect'>" + options + "</select");
于 2014-01-16T22:18:14.050 回答
5
$('#cloneBtn').click(function() {
var $options = $("#myselect > option").clone();
$('#second').empty();
$('#second').append($options);
$('#second').val($('#myselect').val());
});
这用于复制值和innerHTML。最好复制键、值和选项。即选定的值和选项。
于 2014-02-10T05:09:11.033 回答