<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
select{
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<select name="" id="sel1" size="2">
<option value="beijing">北京</option>
<option value="tianjin">天津</option>
<option value="shanghai">上海</option>
<option value="guangzhou">广州</option>
</select>
//添加按钮
<input type="button" value="添加" id="addBtn">
//移除按钮
<input type="button" value="移除" id="removeBtn">
第二个select
<select name="" id="sel2" size="2"></select>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script>
//A.appendTo(B) 把A插入到B的末尾
/*$('#addBtn').click(function () {
$('#sel1 :selected').appendTo('#sel2');//$('#sel1 option:selected')
});
$('#removeBtn').click(function () {
$('#sel2 :selected').appendTo('#sel1');
})*/
//本身存在
$('#addBtn').click(function () {
var res = $('#sel1 :selected').html();
var flag = true;
$('#sel2 option').each(function () {
//遍历
var txt = $(this).html();
if(txt===res){
flag = false;
}
});
if(flag){
$('#sel1 :selected').clone().appendTo('#sel2');
}
});
$('#removeBtn').click(function () {
$('#sel2 :selected').remove();
})
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
select{
width: 300px;
height: 300px;
}
</style>
</head>
<body>
<select name="" id="sel1" size="2">
<option value="beijing">北京</option>
<option value="tianjin">天津</option>
<option value="shanghai">上海</option>
<option value="guangzhou">广州</option>
</select>
//添加按钮
<input type="button" value="添加" id="addBtn">
//移除按钮
<input type="button" value="移除" id="removeBtn">
第二个select
<select name="" id="sel2" size="2"></select>
<script src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script>
//A.appendTo(B) 把A插入到B的末尾
/*$('#addBtn').click(function () {
$('#sel1 :selected').appendTo('#sel2');//$('#sel1 option:selected')
});
$('#removeBtn').click(function () {
$('#sel2 :selected').appendTo('#sel1');
})*/
//本身存在
$('#addBtn').click(function () {
var res = $('#sel1 :selected').html();
var flag = true;
$('#sel2 option').each(function () {
//遍历
var txt = $(this).html();
if(txt===res){
flag = false;
}
});
if(flag){
$('#sel1 :selected').clone().appendTo('#sel2');
}
});
$('#removeBtn').click(function () {
$('#sel2 :selected').remove();
})
</script>
</body>
</html>