selectutil html

 转载自:

http://wallimn.iteye.com/blog/475499

Html代码 复制代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>listbox控制测试</title>  
  6. <script type="text/javascript">  
  7.     function SelectUtil(idOrObj){   
  8.         if(typeof(idOrObj)=="string"){   
  9.             this.selectObj=document.getElementById(idOrObj);   
  10.         }   
  11.         else if (idOrObj!=null && typeof(idOrObj)=="object" && idOrObj.tagName=="SELECT"){   
  12.             this.selectObj=idOrObj;   
  13.         }   
  14.         else{   
  15.             alert("创建对象失败,参数不合法!");   
  16.         }   
  17.     }   
  18.     SelectUtil.prototype.isExist=function(itemValue){   
  19.         var isExist = false;   
  20.         for(var i=0; i<this.selectObj.options.length; i++){   
  21.             if(this.selectObj.options[i].value==itemValue){   
  22.                 isExist=true;   
  23.                 break;   
  24.             }   
  25.         }   
  26.         return isExist;   
  27.     }   
  28.     SelectUtil.prototype.addItem=function(itemText,itemValue){   
  29.         if(!itemText || !itemValue || typeof(itemText)!="string" ||typeof(itemValue)!="string" )return false;   
  30.         if(this.isExist(itemValue)){   
  31.             //alert("项目已存在!");   
  32.             return false;   
  33.         }   
  34.         var optionItem = new Option(itemText,itemValue);   
  35.         this.selectObj.options.add(optionItem);   
  36.         return true;   
  37.     }   
  38.     SelectUtil.prototype.delItem=function(itemValue){   
  39.         var bDel=false;   
  40.         for(var i=0; i<this.selectObj.options.length; i++){   
  41.             if(this.selectObj.options[i].value==itemValue){   
  42.                 bDel=true;   
  43.                 this.selectObj.options.remove(i);   
  44.                 break;   
  45.             }   
  46.         }   
  47.         return bDel;   
  48.     }   
  49.     SelectUtil.prototype.delSelectedItem=function(){   
  50.         var length = this.selectObj.options.length-1;   
  51.         var num = 0;   
  52.         for(var i=length; i>=0; i--){   
  53.             if(this.selectObj.options[i].selected==true){   
  54.                 this.selectObj.options[i] = null;   
  55.                 num++;   
  56.             }   
  57.         }   
  58.         return num;   
  59.     }   
  60.     SelectUtil.prototype.cloneItem = function (itemValue){   
  61.         var result=null;   
  62.         for(var i=0; i<this.selectObj.options.length; i++){   
  63.             if(this.selectObj.options[i].value==itemValue){   
  64.                 result=this.selectObj.options[i];   
  65.                 break;   
  66.             }   
  67.         }   
  68.         if(result==null)return null;   
  69.         return new Option(result.text,result.value);   
  70.     }   
  71.     SelectUtil.prototype.getItem = function (itemValue){   
  72.         var result=null;   
  73.         for(var i=0; i<this.selectObj.options.length; i++){   
  74.             if(this.selectObj.options[i].value==itemValue){   
  75.                 result=this.selectObj.options[i];   
  76.                 break;   
  77.             }   
  78.         }   
  79.         return result;   
  80.     }   
  81.     SelectUtil.prototype.modItemText=function(itemText,itemValue){   
  82.         var opt=this.getItem(itemValue);   
  83.         if(opt==null){   
  84.             alert("没有找到指定的项目!");   
  85.             return false;   
  86.         }   
  87.         else{   
  88.             opt.text = itemText;   
  89.             return true;   
  90.         }   
  91.     }   
  92.     SelectUtil.prototype.selItemByValue=function(itemValue){   
  93.         var opt = this.getItem(itemValue);   
  94.         if(opt!=null){   
  95.             opt.selected=true;   
  96.             return true;   
  97.         }   
  98.         else{   
  99.             return false;   
  100.         }   
  101.     }   
  102.     SelectUtil.prototype.clear=function(){   
  103.         this.selectObj.options.length=0;   
  104.     }   
  105.     SelectUtil.prototype.selectedIndex=function(){   
  106.         return this.selectObj.selectedIndex;   
  107.     }   
  108.     SelectUtil.prototype.seletedText=function(){   
  109.         return this.selectObj.text;   
  110.     }   
  111.     SelectUtil.prototype.getSelectedItem=function(){   
  112.         var idx = this.selectObj.selectedIndex;   
  113.         if(idx==-1)return null;   
  114.         else{   
  115.             return this.selectObj.options[idx];   
  116.         }   
  117.     }   
  118.     SelectUtil.prototype.adjustItem=function(optionObj,direction){   
  119.         if(!optionObj){   
  120.             optionObj = this.getSelectedItem();   
  121.         }   
  122.         if(!optionObj)return false;   
  123.         var delta = (direction=="down")?1:-1;   
  124.         if(optionObj.index+delta<0 || optionObj.index+delta>=this.selectObj.options.length)return true;   
  125.         else{   
  126.             var opt,tmp;   
  127.             opt = this.selectObj.options[optionObj.index+delta];   
  128.             tmp = opt.value;   
  129.             opt.value=optionObj.value;   
  130.             optionObj.value = tmp;   
  131.             tmp = opt.text;   
  132.             opt.text=optionObj.text;   
  133.             optionObj.text = tmp;   
  134.             opt.selected=true;   
  135.             optionObj.selected=false;   
  136.             return true;   
  137.         }   
  138.     }   
  139.     SelectUtil.prototype.getAllItem=function(){   
  140.         return this.selectObj.options;   
  141.     }   
  142.     SelectUtil.prototype.getItemCount=function(){   
  143.         return this.selectObj.options.length;   
  144.     }   
  145.     SelectUtil.prototype.moveSelectedItemTo=function(anotherSelectObj){   
  146.         if(!anotherSelectObj)return false;   
  147.         var length = this.selectObj.options.length-1;   
  148.         var num = 0,opt;   
  149.         for(var i=length; i>=0; i--){   
  150.             if(this.selectObj.options[i].selected==true){   
  151.                 num++;   
  152.                 opt = this.selectObj.options[i];   
  153.                 //没有验证有无重复   
  154.                 anotherSelectObj.options.add(new Option(opt.text,opt.value));   
  155.                 this.selectObj.options[i] = null;   
  156.             }   
  157.         }   
  158.         return num;   
  159.     }   
  160.     SelectUtil.prototype.moveAllItemTo=function(anotherSelectObj,bCreate){   
  161.         if(!anotherSelectObj)return false;   
  162.         var length = this.selectObj.options.length-1;   
  163.         var num = 0,opt=null;   
  164.         for(var i=length; i>=0; i--){   
  165.             num++;   
  166.             opt = this.selectObj.options[i];   
  167.             //没有验证有无重复   
  168.             anotherSelectObj.options.add(new Option(opt.text,opt.value));   
  169.             this.selectObj.options[i] = null;   
  170.         }   
  171.         return num;   
  172.     }   
  173.     SelectUtil.prototype.getObject=function(){   
  174.         return this.selectObj;   
  175.     }   
  176.     SelectUtil.prototype.selectAll=function(){   
  177.         for(var i=0; i<this.selectObj.options.length; i++){   
  178.             this.selectObj.options[i].selected=true;   
  179.         }   
  180.     }   
  181. </script>  
  182. <style type="text/css">  
  183.     #srclb,#dstlb{   
  184.         border:1px solid #aaa;   
  185.         width:200px;   
  186.         height:400px;   
  187.     }   
  188.     .zhcxbtn{   
  189.         width:30px;   
  190.     }   
  191. </style>  
  192. </head>  
  193.   
  194. <body>  
  195. <div>  
  196. <table width="460" border="0" class="zhcx" cellpadding="0" cellspacing="0">  
  197.     <tr>  
  198.         <td>  
  199.             <select multiple="multiple" name="srclb" id="srclb" ondblclick="srclb.moveSelectedItemTo(dstlb.getObject());">  
  200.                 <option value="1">宝马1</option>  
  201.                 <option value="2">宝马2</option>  
  202.                 <option value="3">宝马3</option>  
  203.                 <option value="4">宝马4</option>  
  204.                 <option value="5">宝马5</option>  
  205.                 <option value="6">宝马6</option>  
  206.                 <option value="7">宝马7</option>  
  207.             </select>  
  208.         </td>  
  209.         <td>  
  210.             <input type="button" class="zhcxbtn" value=">" onclick="srclb.moveSelectedItemTo(dstlb.getObject());"/>  
  211.             <input type="button" class="zhcxbtn" value=">>" onclick="srclb.moveAllItemTo(dstlb.getObject());"/>  
  212.             <input type="button" class="zhcxbtn" value="<" onclick="dstlb.moveSelectedItemTo(srclb.getObject());"/>  
  213.             <input type="button" class="zhcxbtn" value="<<" onclick="dstlb.moveAllItemTo(srclb.getObject());"/>  
  214.         </td>  
  215.         <td>  
  216.             <select multiple="multiple" name="dstlb" id="dstlb" ondblclick="dstlb.adjustItem();">  
  217.             </select>  
  218.         </td>  
  219.         <td>  
  220.             <input type="button" class="zhcxbtn" value="↑" onclick="dstlb.adjustItem();"/>  
  221.             <input type="button" class="zhcxbtn" value="↓" onclick="dstlb.adjustItem(null,'down');"/>  
  222.         </td>  
  223.     </tr>  
  224. </table>  
  225. </div>  
  226. <input type="button" value="全选" onclick="dstlb.selectAll();"/>  
  227. <script type="text/javascript">  
  228.     var dstlb = new SelectUtil("dstlb");   
  229.     var srclb = new SelectUtil("srclb");   
  230. </script>  
  231. </body>  
  232. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>listbox控制测试</title>
<script type="text/javascript">
	function SelectUtil(idOrObj){
		if(typeof(idOrObj)=="string"){
			this.selectObj=document.getElementById(idOrObj);
		}
		else if (idOrObj!=null && typeof(idOrObj)=="object" && idOrObj.tagName=="SELECT"){
			this.selectObj=idOrObj;
		}
		else{
			alert("创建对象失败,参数不合法!");
		}
	}
	SelectUtil.prototype.isExist=function(itemValue){
		var isExist = false;
		for(var i=0; i<this.selectObj.options.length; i++){
			if(this.selectObj.options[i].value==itemValue){
				isExist=true;
				break;
			}
		}
		return isExist;
	}
	SelectUtil.prototype.addItem=function(itemText,itemValue){
		if(!itemText || !itemValue || typeof(itemText)!="string" ||typeof(itemValue)!="string" )return false;
		if(this.isExist(itemValue)){
			//alert("项目已存在!");
			return false;
		}
		var optionItem = new Option(itemText,itemValue);
		this.selectObj.options.add(optionItem);
		return true;
	}
	SelectUtil.prototype.delItem=function(itemValue){
		var bDel=false;
		for(var i=0; i<this.selectObj.options.length; i++){
			if(this.selectObj.options[i].value==itemValue){
				bDel=true;
				this.selectObj.options.remove(i);
				break;
			}
		}
		return bDel;
	}
	SelectUtil.prototype.delSelectedItem=function(){
		var length = this.selectObj.options.length-1;
		var num = 0;
		for(var i=length; i>=0; i--){
			if(this.selectObj.options[i].selected==true){
				this.selectObj.options[i] = null;
				num++;
			}
		}
		return num;
	}
	SelectUtil.prototype.cloneItem = function (itemValue){
		var result=null;
		for(var i=0; i<this.selectObj.options.length; i++){
			if(this.selectObj.options[i].value==itemValue){
				result=this.selectObj.options[i];
				break;
			}
		}
		if(result==null)return null;
		return new Option(result.text,result.value);
	}
	SelectUtil.prototype.getItem = function (itemValue){
		var result=null;
		for(var i=0; i<this.selectObj.options.length; i++){
			if(this.selectObj.options[i].value==itemValue){
				result=this.selectObj.options[i];
				break;
			}
		}
		return result;
	}
	SelectUtil.prototype.modItemText=function(itemText,itemValue){
		var opt=this.getItem(itemValue);
		if(opt==null){
			alert("没有找到指定的项目!");
			return false;
		}
		else{
			opt.text = itemText;
			return true;
		}
	}
	SelectUtil.prototype.selItemByValue=function(itemValue){
		var opt = this.getItem(itemValue);
		if(opt!=null){
			opt.selected=true;
			return true;
		}
		else{
			return false;
		}
	}
	SelectUtil.prototype.clear=function(){
		this.selectObj.options.length=0;
	}
	SelectUtil.prototype.selectedIndex=function(){
		return this.selectObj.selectedIndex;
	}
	SelectUtil.prototype.seletedText=function(){
		return this.selectObj.text;
	}
	SelectUtil.prototype.getSelectedItem=function(){
		var idx = this.selectObj.selectedIndex;
		if(idx==-1)return null;
		else{
			return this.selectObj.options[idx];
		}
	}
	SelectUtil.prototype.adjustItem=function(optionObj,direction){
		if(!optionObj){
			optionObj = this.getSelectedItem();
		}
		if(!optionObj)return false;
		var delta = (direction=="down")?1:-1;
		if(optionObj.index+delta<0 || optionObj.index+delta>=this.selectObj.options.length)return true;
		else{
			var opt,tmp;
			opt = this.selectObj.options[optionObj.index+delta];
			tmp = opt.value;
			opt.value=optionObj.value;
			optionObj.value = tmp;
			tmp = opt.text;
			opt.text=optionObj.text;
			optionObj.text = tmp;
			opt.selected=true;
			optionObj.selected=false;
			return true;
		}
	}
	SelectUtil.prototype.getAllItem=function(){
		return this.selectObj.options;
	}
	SelectUtil.prototype.getItemCount=function(){
		return this.selectObj.options.length;
	}
	SelectUtil.prototype.moveSelectedItemTo=function(anotherSelectObj){
		if(!anotherSelectObj)return false;
		var length = this.selectObj.options.length-1;
		var num = 0,opt;
		for(var i=length; i>=0; i--){
			if(this.selectObj.options[i].selected==true){
				num++;
				opt = this.selectObj.options[i];
				//没有验证有无重复
				anotherSelectObj.options.add(new Option(opt.text,opt.value));
				this.selectObj.options[i] = null;
			}
		}
		return num;
	}
	SelectUtil.prototype.moveAllItemTo=function(anotherSelectObj,bCreate){
		if(!anotherSelectObj)return false;
		var length = this.selectObj.options.length-1;
		var num = 0,opt=null;
		for(var i=length; i>=0; i--){
			num++;
			opt = this.selectObj.options[i];
			//没有验证有无重复
			anotherSelectObj.options.add(new Option(opt.text,opt.value));
			this.selectObj.options[i] = null;
		}
		return num;
	}
	SelectUtil.prototype.getObject=function(){
		return this.selectObj;
	}
	SelectUtil.prototype.selectAll=function(){
		for(var i=0; i<this.selectObj.options.length; i++){
			this.selectObj.options[i].selected=true;
		}
	}
</script>
<style type="text/css">
	#srclb,#dstlb{
		border:1px solid #aaa;
		width:200px;
		height:400px;
	}
	.zhcxbtn{
		width:30px;
	}
</style>
</head>

<body>
<div>
<table width="460" border="0" class="zhcx" cellpadding="0" cellspacing="0">
	<tr>
    	<td>
            <select multiple="multiple" name="srclb" id="srclb" ondblclick="srclb.moveSelectedItemTo(dstlb.getObject());">
                <option value="1">宝马1</option>
                <option value="2">宝马2</option>
                <option value="3">宝马3</option>
                <option value="4">宝马4</option>
                <option value="5">宝马5</option>
                <option value="6">宝马6</option>
                <option value="7">宝马7</option>
            </select>
        </td>
    	<td>
            <input type="button" class="zhcxbtn" value=">" onclick="srclb.moveSelectedItemTo(dstlb.getObject());"/>
            <input type="button" class="zhcxbtn" value=">>" onclick="srclb.moveAllItemTo(dstlb.getObject());"/>
            <input type="button" class="zhcxbtn" value="<" onclick="dstlb.moveSelectedItemTo(srclb.getObject());"/>
            <input type="button" class="zhcxbtn" value="<<" onclick="dstlb.moveAllItemTo(srclb.getObject());"/>
        </td>
    	<td>
            <select multiple="multiple" name="dstlb" id="dstlb" ondblclick="dstlb.adjustItem();">
            </select>
        </td>
    	<td>
            <input type="button" class="zhcxbtn" value="↑" onclick="dstlb.adjustItem();"/>
            <input type="button" class="zhcxbtn" value="↓" onclick="dstlb.adjustItem(null,'down');"/>
        </td>
    </tr>
</table>
</div>
<input type="button" value="全选" onclick="dstlb.selectAll();"/>
<script type="text/javascript">
	var dstlb = new SelectUtil("dstlb");
	var srclb = new SelectUtil("srclb");
</script>
</body>
</html>

 

你可能感兴趣的:(JavaScript,html,XHTML,css,prototype)