JQuery操作listbox原理并不难,就是将listbox中的选中项进行移动,实现我们需要的移动效果。我在例子中使用了json数据结构来动态绑定listbox,这样也可以熟悉一下JSON的使用方法。
先看看简单的html,因为服务器控件会自动转换为HTML标签,所以在例子中,我并没有用服务器控件。如下:
<asp:ListBox></asp:ListBox>
会自动转换成:<select></select>
html代码如下:
<div style="width:240px;"> <div style="width:100px;float:left;"> <select size="4" name="listLeft" id="listLeft" class="normal"> </select> </div> <div style="width:40px;float:left; padding-top:20px;"> <input type="button" id="BTnRight" value=">>" /><br /> <input type="button" id="btnLeft" value="<<" /> </div> <div style="width:100px;float:left;"> <select size="4" name="listRight" id="listRight" class="normal"> </select> </div> </div>
//bind data var vlist = ""; //遍历json数据 JQuery.each(vData.datalist, function(i, n) { vlist += "<option value=" + n.data + ">" + n.text + "</option>"; }); //绑定数据到listLeft $("#listLeft").append(vlist); //left move $("#btnRight").click(function() { //数据option选中的数据集合赋值给变量vSelect var vSelect = $("#listLeft option:selected"); //克隆数据添加到listRight中 vSelect.clone().APPendTo("#listRight"); //同时移除listRight中的option vSelect.remove(); }); //right move $("#btnLeft").click(function() { var vSelect = $("#listRight option:selected"); vSelect.clone().appendTo("#listLeft"); vSelect.remove(); });







网友评论文明上网理性发言 已有0人参与
发表评论: