function upOrDown(flag){ var $List = $(#id‘); var $selectedList = $List.find(‘option:selected‘); var len = $selectedList.length; if(!len){ // 没有选择,不允许上下移动 alert(‘请先选中需要移动的指标‘); return; } var index = $selectedList[0].index; var $optionArr = $List.find(‘option‘); if(!flag){ if(index == 0){ return ; } $selectedList.insertBefore($selectedList.prev(‘option‘)); //changeObj(optionArr[index],optionArr[index - 1]); }else{ if(index == $optionArr.length - 1){ return; } $selectedList.insertAfter($selectedList.next(‘option‘)); //changeObj(optionArr[index],optionArr[index + 1]); } } /** * 交换两个Select中的Option对象 */ function changeObj(sourceObj,targetObj){ // 源对象的值与文本 var sourceVal = sourceObj.value; var sourceText = sourceObj.text; var sourceOther = sourceObj[‘other‘]; // 目标对象的值与文本 var targetVal = targetObj.value; var targetText = targetObj.text; var targetOther = targetObj[‘other‘]; // 将目标对象赋给源对象 sourceObj.value = targetVal; sourceObj.text = targetText; sourceObj[‘other‘] = targetOther; sourceObj.selected = false; // 将源对象赋给目标对象 targetObj.value = sourceVal; targetObj.text = sourceText; targetObj[‘other‘] = sourceOther; targetObj.selected = true; }
时间: 2024-10-26 13:40:05