当前位置 : 主页 > 网络编程 > JavaScript >

js实现左右移动两个下拉框的数据以及上下排序

来源:互联网 收集:自由互联 发布时间:2021-06-28
js /** * Created by Administrator on 2017/10/24. */var move = function (obj) { if($("#sel1").find("option:selected").val()==undefined $("#sel2").find("option:selected").val()==undefined){ layer.msg("请点击选择字段后操作", {time: 2
js
/**
 * Created by Administrator on 2017/10/24.
 */
var move = function (obj) {
    if($("#sel1").find("option:selected").val()==undefined && $("#sel2").find("option:selected").val()==undefined){
        layer.msg("请点击选择字段后操作", {time: 2000});
        return;
    }
    var sel1 = document.getElementById('sel1');
    var sel2 = document.getElementById('sel2');

    if (obj.value == '>>') {
        for (var i = 0; i < document.getElementById('sel2').options.length; i++) {
            if ($("#sel1").val() == document.getElementById('sel2').options[i].value) {
                layer.msg("该字段已被选择!请勿重复", {time:2000});
                return;
            }
        }

        for (var i = 0; i < sel1.children.length; i++) {
            var x = sel1.children[i];
            if (x.selected) {
                sel2.appendChild(x);
                i = -1;
                continue;
            }
        }
    }
    else if (obj.value == '<<') {
        for (var i = 0; i < document.getElementById('sel1').options.length; i++) {
            if (document.getElementById('sel1').options[i].value== $("#sel2").val()) {
                // document.getElementById('sel1').options[i].remove();
                //$("#sel2 option[value='3']").remove();
                $("#sel2").find("option:selected").remove();
            }
        }
        for (var i = 0; i < sel2.children.length; i++) {
            var x = sel2.children[i];
            if (x.selected) {
                sel1.appendChild(x);
                i = -1;
                continue;
            }
        }
    }
}


function moveSort(to) {
    var list = document.getElementById('sel2');
    var total = list.options.length - 1;

    var index = document.getElementById('sel2').selectedIndex;
    /*alert(to);
     alert("index=" + index);*/

    if (index == -1) return false;

    if (to == +1 && index == total) return false;
    if (to == -1 && index == 0) return false;
    var items = new Array;
    var values = new Array;

    if (to == 0) {
        for (i = total; i >= 0; i--) {
            items[i] = list.options[i].text;
            values[i] = list.options[i].value;
        }
        //置顶
        var t = document.getElementById('sel2');
//                alert(t.selectedIndex);
//                t.selectedIndex=0;
        for (i = 0; i <= total; i++) {//index1
            if (index > i) {
                list.options[i + 1] = new Option(items[i], values[i]);
            }
            if (index == i) {
                list.options[0] = new Option(items[i], values[i]);
            }
            if (index < i) {
                list.options[i] = new Option(items[i], values[i]);
            }
        }
    } else {
        for (i = total; i >= 0; i--) {
            items[i] = list.options[i].text;
            values[i] = list.options[i].value;
        }
        for (i = total; i >= 0; i--) {
            if (index == i) {
                list.options[i + to] = new Option(items[i], values[i], 0, 1);
                list.options[i] = new Option(items[i + to], values[i + to]);
                i--;
            }
            else {
                list.options[i] = new Option(items[i], values[i]);
            }
        }
        list.focus();
    }
}
jsp
 
    
  
    
      行业字段 新增字段 <%--新增字段--%>   
    
    
     
      
      
        
          
        
        
         
        
      
       
      已选字段    
    
  
 
网友评论