使用jQuery实现option的上移和下移

基本思路:
    上移:(1)获取当前选中的元素的索引值
            (2)判断当前元素是否为第一个元素
            (3)如果是,则不执行上移操作,如果不是,则则调用insertBefore方法插入到他的prev(紧邻的上一个)元素之前
1 var up = function () {
2          var selectedIndex = $("#SelectedAddressIds option:selected").index(); //获取当前选中元素的索引
3          if(selectedIndex >= 1){
4                   // 插入上一个
5                $("#SelectedAddressIds option:selected").insertBefore($("#SelectedAddressIds option:selected").prev('option'));
6          }
7 }
  下移:(1)获取所有option元素的索引值
              (2)获取当前选中元素的索引值
              (3)判断当前选中元素是否为最后一个元素,如果是,则不执行下移,如果不是则调用insertAfter方法插入到他的next(紧邻的下一个)元素的后面。
 1 var down = function () {
 2             // 获取最后一个option的索引值
 3             var optionNum = $("#SelectedAddressIds option").size() - 1;
 4             // 获取当前选中元素的索引值
 5             var selectedIndex = $("#SelectedAddressIds option:selected").index();
 6 
 7             if(selectedIndex < 6){
 8                 // 插入下一个
 9                 $("#SelectedAddressIds option:selected").insertAfter($("#SelectedAddressIds option:selected").next('option'));
10             }
11 }

 

posted @ 2016-10-18 20:27  雨帘轻挂  阅读(576)  评论(0编辑  收藏  举报