+ -
当前位置:首页 → 问答吧 → 帮忙精简一下代码

帮忙精简一下代码

时间:2009-03-25

来源:互联网

本菜鸟刚接触jquery,项目中需要写一个控制select标签中的options向上移动或向下移动的效果,我用jquery试着写了一个,但是很臃肿,请大侠帮忙看看哪里可以精简一下,先谢谢了
这个是页面代码
复制代码
  1.         <form action="" method="post">
  2.             <select size="4" id="sel">
  3.                 <option>1回家</option>
  4.                 <option>2出租</option>
  5.                 <option>3考虑</option>
  6.                 <option>4票据</option>
  7.                 <option>5即发</option>
  8.                 <option>6打开</option>
  9.             </select>
  10.             <input type="button" value="向上" onclick="move(0)"/><input type="button" value="向下" onclick="move(1)" />
  11.         </form>

这个是function
复制代码
  1.     function move(num){
  2.         switch (num){
  3.             case(0):
  4.                 var tempNum = $("#sel").val();
  5.                 var tempText = $("#sel :selected").text();
  6.                 var index = $("#sel")[0].selectedIndex;
  7.                 if(index!=0){
  8.                     var tempT= $("#sel :selected").prev().text();
  9.                     var tempV= $("#sel :selected").prev().val();
  10.                     $("#sel :selected").prev().text(tempText);
  11.                     $("#sel :selected").prev().val(tempNum);
  12.                     $("#sel :selected").val(tempV);
  13.                     $("#sel :selected").text(tempT);
  14.                     $("#sel")[0].selectedIndex = index-1;
  15.                 }
  16.                 break;
  17.             case(1):
  18.                 var tempNum = $("#sel").val();
  19.                 var tempText = $("#sel :selected").text();
  20.                 var index = $("#sel")[0].selectedIndex;
  21.                 if(index!=$("#sel")[0].length-1){
  22.                     var tempT= $("#sel :selected").next().text();
  23.                     var tempV= $("#sel :selected").next().val();
  24.                     $("#sel :selected").text(tempT);
  25.                     $("#sel :selected").val(tempV);
  26.                     $("#sel :selected").next().val(tempNum);
  27.                     $("#sel :selected").next().text(tempText);
  28.                     $("#sel")[0].selectedIndex = index+1;
  29.                 }
  30.                 break;
  31.         }
  32.         
  33.     }

作者: signs   发布时间: 2009-03-25

嗯,我是想不通项目怎么会这样做,有点不可思议,呵呵

<select size="4" id="sel">
<option>1回家</option>
<option>2出租</option>
<option>3考虑</option>
<option>4票据</option>
<option>5即发</option>
<option>6打开</option>
</select>
<button id="up">up</button>
<button id="down">down</button>

     $("#down").click(function(){
        var option = $("#sel option:selected");
        option.next("option").after(option);
     });
    
     $("#up").click(function(){
        var option = $("#sel option:selected");
        option.prev("option").before(option);
     });

作者: gordianyuan   发布时间: 2009-03-25

多谢大侠,代码很好使。没法子,甲方的要求的,我也是想不通

作者: signs   发布时间: 2009-03-25

对于一种排序,还是勉强可以理解的……
以前写单机程序的时候也搞过这个

作者: shawphy   发布时间: 2009-03-25