select 左右選擇(js)
阿新 • • 發佈:2018-12-30
Select左右選擇!!!(改樣式後可以直接用)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>select左右選擇</title>
<style type="text/css">
.select_margin {margin-left:4px;float:left;}
.input_margin {margin-left:4px;margin-top:20px;float :left;}
.select_win {padding: 0 0 0 2px; height: 200px;width: 166px;}
</style>
</head>
<body>
<span class="select_margin" style="margin-left:50px;">
<div class="tdlabel_net">選擇列表</div>
<div>
<select class="select_win" name="addName" id="addName" multiple="" ondblclick="javascript:addSel(addName,delName,3);">
<option value="aaa">aaa</option>
<option value="bbb">bbb</option>
<option value="ccc">ccc</option>
<option value="ddd">ddd</option>
</select >
</div>
</span>
<span class="input_margin">
<input type="button" onClick="javascript:addSel(addName,delName,3);" value=" > " ><br><br>
<input type="button" onClick="javascript:delSel(addName,delName);" value=" x " >
</span>
<span class="select_margin">
<div class="tdlabel_net">已經選擇</div>
<div>
<select class="select_win" name="delName" id="delName" multiple="" ondblclick="javascript:delSel(addName,delName);"></select>
</div>
</span>
</body>
</html>
<script type="text/javascript">
function addSel(srcAddrSel,srcSelected,maxMember){
var len=srcSelected.length;
for(var i=0;i<srcAddrSel.length;i++){
if((srcAddrSel.options[i]!=null)&&(srcAddrSel.options[i].selected)){
if(len>=maxMember){
alert("已選擇的成員不能超過"+maxMember+"個!");
break;
}
srcSelected.options[len]=new Option(srcAddrSel.options[i].value);
srcSelected.options[len].value=srcAddrSel.options[i].value;
srcSelected.options[len].text=srcAddrSel.options[i].text;
srcSelected.options[len].selected=true;
srcAddrSel.remove(i);
len++;
i=i-1
}
}
}
function delSel(srcAddrSel,srcSelected){
console.log(srcAddrSel);
var len=srcSelected.options.length;
var length=srcAddrSel.options.length;
for(var i=0;i<len;i++){
if((srcSelected.options[i]!=null)&&(srcSelected.options[i].selected==true)){
srcAddrSel.options[length]=new Option(srcSelected.options[i].value);
srcAddrSel.options[length].value=srcSelected.options[i].value;
srcAddrSel.options[length].text=srcSelected.options[i].text;length++
}
}
for(var i=(len-1);i>=0;i--){
if((srcSelected.options[i]!=null)&&(srcSelected.options[i].selected)){
srcSelected.options[i]=null;
}
}
}
</script>