日期:2014-05-16 浏览次数:20486 次
// >>,假设左边的选框ID为id1,右边的为id2,内部由DIV构成
// DIV点击事件
$("div").bind("click", function() {
var leftContain = $("#id1");
var rightContain = $("#id2");
var slt = $(this);
$(this).appendTo(id2);
$(this).remove();
});
#container{
width:700px;
margin:0 auto;
overflow:hidden;
}
.unselected{
float:left;
width:300px;
}
.select-btn{
float:left;
width:100px;
height:100%;
text-align:center;
}
.selected{
float:left;
width:300px;
}
select{
width:300px;
}
<div id="container">
<div class="unselected">
<select size="8" multiple="multiple" id="unselected"></select>
</div>
<div class="select-btn">
<button type="button" id="selectall">>></button><br />
<button type="button" id="selectone">></button><br />
<button type="button" id="removeone"><</button><br />
<button type="button" id="removeall"><<</button>
</div>
<div class="selected">
<select size="8" multiple="multiple" id="selected"></select>
</div>
</div>
var data=[{label:"item1",value:1},{label:"item2",value:2},{label:"item3",value:3},{label:"item4",value:4},{label:"item5",value:5},{label:"item6",value:6},{label:"item7",value:7},{label:"item8",value:8},{label:"item9",value:9},{label:"item10",value:10},{label:"item11",value:11}];
//初始化
for(var i=0;i<data.length;i++){
var item=document.createElement("option");
item.text=data[i].label;
item.value=data[i].value;
item.initIndex=i;
unselected.appendChild(item);
}
//选择全部
selectall.addEventListener("click",function(){
var items=[];
for(var i=0;i<unselected.length;i++){
items.push(unselected[i]);
}
select(items);
},false);
//选择部分
selectone.addEventListener("click",function(){
var items=[];
for(var i=0;i<unselected.length;i++){
if(unselected[i].selected){
items.push(unselected[i]);
}
}
select(items);
},false);
//移除全部
removeall.addEventListener("click",function(){
var items=[];
for(var i=0;i<selected.length;i++){
items.push(selected[i]);
}
remove(items);
},false);
//移除部分
removeone.addEventListener("click",function(){
var items=[];