用jquery控制listbox中项的移动,并排序

  以下是用jquery控制ASP.NET中的两个asp:listbox控件中选择项的移动:

首先是html代码,页面上放2个listbox控件和2个按钮用于移动项目

ExpandedBlockStart.gif 代码
   < table  border ="0" >< tr >< td  width ="156" > 全部水果: </ td >< td  width ="142" > &nbsp; </ td >< td  width ="482" > 我挑选的: </ td ></ tr >< tr >< td  rowspan ="2" >< asp:ListBox  SelectionMode ="Multiple"  ID ="listall"  Rows ="12"  Width ="156"  runat ="server" ></ asp:ListBox ></ td >< td  height ="41"  align ="center" >< input  type ="button"  id ="btnleftmove"  value ="&gt;&gt;&gt;"  onclick ="move('listall','listmy');" />< br  />< br  />< input  type ="button"  id ="btnrighttmove"  value ="&lt;&lt;&lt;"  onclick ="move('listmy','listall');" /></ td >< td  rowspan ="2" >< asp:ListBox  SelectionMode ="Multiple"  ID ="listmy"  Rows ="12"  Width ="156"  runat ="server" ></ asp:ListBox ></ td ></ tr ></ table >   
  
<!--  这里放个隐藏域用于取值  -->
  
< input  id ="hidValue"  runat ="server"  type ="hidden"   />  
ExpandedBlockStart.gif

  

    

    

    

  

  
 
    

    

    

    

    

    

  

下面是在.cs文件中绑定一些数据

ContractedBlock.gif ExpandedBlockStart.gif Code
public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            BindData();
        }
    }

    
private void BindData()
    {
        ArrayList list=DataArray();
        
for (int i = 0; i < list.Count; i++)
        {
            listall.Items.Add(list[i].ToString());
            listall.Items[i].Attributes["tag"= i.ToString(); //用tag记录排序字段
        }
    }

    
private ArrayList DataArray()
    {
        
//用到的一些数据,这里已默认按第一个字的拼音排序
        ArrayList list = new ArrayList();
        list.Add("草莓");
        list.Add("");
        list.Add("桔子");
        list.Add("芒果");
        list.Add("苹果");
        list.Add("香蕉");
        
return list;
    }
}

在实际使用时可根据数据库中的字段排序

下面是jquery的代码:

ExpandedBlockStart.gif 代码
// 移动用户选择的角色 //setname:要移出数据的列表名称 getname:要移入数据的列表名称function move(setname,getname)
{
 
var  size = $( " # " + setname + "  option " ).size();
 
var  selsize = $( " # " + setname + "  option:selected " ).size();
 
if (size > 0 && selsize > 0 )
 {
     $.each($(
" # " + setname + "  option:selected " ),  function (id,own){
     
var  text = $(own).text();
     
var  tag = $(own).attr( " tag " );
     $(
" # " + getname).prepend( " <option tag=\ "" +tag+ " \ " > " + text + " </option> " ); 
     $(own).remove(); 
     $(
" # " + setname + "" ).children( " option:first " ).attr( " selected " , true );

});       
    
// 赋值到hidvalue中
         var  strvalue = "" ;  
        $.each($(
" #listmy option " ),  function (id,own){
            strvalue
+= $(own).val();
            
if (id != $( " #listmy option " ).size() - 1 )
                strvalue
+= " , " ;
        });
        $(
" #hidUserRole " ).val(strvalue);
 }

 
// 重新排序   
 $.each($( " # " + getname + "  option " ),  function (id,own){
       orderrole(getname);
     });
}

// 按首字母排序角色列表function orderrole(listname)
{
 
var  size = $( " # " + listname + "  option " ).size();
 
var  one = $( " # " + listname + "  option:first-child " );  
 
if (size > 0 )
 {
     
var  text = $(one).text();
     
var  tag = parseInt($(one).attr( " tag " ));
     
// 循环列表中第一项值下所有元素     $.each($(one).nextAll(), function(id,own){
       var  nextag = parseInt($(own).attr( " tag " ));
      
if (tag > nextag)
     { 
      $(one).remove();
      $(own).after(
" <option tag=\ "" +tag+ " \ " > " + text + " </option> " );
      one
= $(own).next();
     }
     });
 }
}

 

这样就完成了简单的js控制两个列表项的值移动。

转载于:https://www.cnblogs.com/kaka8384/archive/2009/04/14/1432628.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值