js写的双向选择器 案例(jsp页面,json获取数据)

2 篇文章 0 订阅
1 篇文章 0 订阅
1. 双向选择器,略有修改,效果见附件图,jsp页面:

 
<table align="center" width="80%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="40%">
请选择<br/>
<select name="s1" size="15" multiple="multiple" id="s1" style="width:100%;">
</select>
</td>
<td width="20%" align="center">
<input type="button" name="add" id="add" value=" > " />

<input type="button" name="addall" id="addall" value=">> " />

<input type="button" name="remove" id="remove" value=" < " />

<input type="button" name="removeall" id="removeall" value="<< " />
</td>
<td width="40%">
已选择<br/>
<select name="s2" size="15" multiple="multiple" id="s2" style=" width:100%;"> </select>
</td>
</tr>
</table><br/><br/>
<div align="center">
<button id="updaterole" type="button">保存</button>  <button type="button" id="close">关闭</button>

<!--    
<input id="reset" type="reset" value="返回"/> -->
</div>
</form>
</div>



2. js代码:(在同一jsp页面)
双击可进行选择
用ajax获取待选项与已选项,已选择json返回格式为:

[{"name":"超级管理员","id":1,"description":"超级管理员","status":1,"createDatetime":"2011-12-08 15:20:15","createBy":1,"updateDatetime":"2011-12-08 15:20:19","updateBy":1}]


 
<script type="text/javascript">
$("#s1").html("");
$("#s2").html("");
//请选择角色
$.ajax({
url: "services/sys/sel-wait.do",
data: {"operid":id},
success: function(data) {
for(var i=0;i<data.length;i++){
$("#s1").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
}
},
error: function() {

}
});
//已选择角色
$.ajax({
url: "services/sys/sel-select.do",
data: {"operid":id},
success: function(data) {
for(var i=0;i<data.length;i++){
$("#s2").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
}
},
error: function() {

}
});

//$("#s1 option:first,#s2 option:first").attr("selected",true);
$("#s1").dblclick(function(){
$("option:selected",this).clone().appendTo("#s2");
$("option:selected",this).remove();
//$("#s1 option:first,#s2 option:first").attr("selected",true);
});

$("#s2").dblclick(function(){
$("option:selected",this).clone().appendTo("#s1");
$("option:selected",this).remove();
//$("#s1 option:first,#s2 option:first").attr("selected",true);
});

$("#add").click(function(){
$("#s1 option:selected").clone().appendTo("#s2");
$("#s1 option:selected").remove();
//$("#s1 option:first,#s2 option:first").attr("selected",true);
});

$("#remove").click(function(){
$("#s2 option:selected").clone().appendTo("#s1");
$("#s2 option:selected").remove();
//$("#s1 option:first,#s2 option:first").attr("selected",true);
});

$("#addall").click(function(){
$("#s1 option").clone().appendTo("#s2");
$("#s1 option").remove();
//$("#s1 option:first,#s2 option:first").attr("selected",true);
});

$("#removeall").click(function(){
$("#s2 option").clone().appendTo("#s1");
$("#s2 option").remove();
//$("#s1 option:first,#s2 option:first").attr("selected",true);
});

</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值