1. 程式人生 > >jquery以及js實現option左移右移

jquery以及js實現option左移右移

[html] view plain copy print?
  1. <tablecellspacing="1"width="350px"align="center">
  2.                    <tr>
  3.                     <td>
  4.                     <tablestyle="background-color:white"width="100%">
  5.                         <tr>
  6.                             <td>
  7.                                 <
    fieldset>
  8.                                     <legend>稽核人員</legend>
  9.                                     <selectname="left_select"multiple="multiple"size="10"id="left_select"style="width : 152px">
  10.                                     </select>
  11.                                  </fieldset
    >
  12.                             </td>
  13.                             <td>
  14.                                 <inputtype="button"value="<"style="font-size:10pt;width:35px"onclick="left()"><br>
  15.                                 <inputtype="button"value="<<"style="font-size:10pt;width:35px"
    onclick="left(true)"><br>
  16.                                 <inputtype="button"value=">"style="font-size:10pt;width:35px"onclick="right()"><br>
  17.                                 <inputtype="button"value=">>"style="font-size:10pt;width:35px"onclick="right(true)"><br>
  18.                             </td>
  19.                             <td>
  20.                                 <fieldset>
  21.                                     <legend>系統人員</legend>
  22.                                     <selectname="right_select"multiple="multiple"style="width : 152px"size="10"id="right_select">
  23.                                         <optionvalue="zhangsan">zhangsan</option>
  24.                                         <optionvalue="lisi">lisi</option>
  25.                                         <optionvalue="lisi">lisi</option>
  26.                                         <optionvalue="wangwu">wangwu</option>
  27.                                     </select>
  28.                                 </fieldset>
  29.                             </td>
  30.                         </tr>
  31.                     </table>
  32.                     </td>
  33.                  </tr>
  34.              </table>
<table cellspacing="1" width="350px" align="center">
			       <tr>
			        <td>
			        <table style="background-color:white" width="100%">
			            <tr>
			                <td>
				                <fieldset>
				                	<legend>稽核人員</legend>
					                <select name="left_select" multiple="multiple" size="10" id="left_select" style="width : 152px">
					                </select>
					             </fieldset>
			                </td>
			                <td>
			              		<input type="button" value="<" style="font-size:10pt;width:35px" onclick="left()"><br>
			              		<input type="button" value="<<" style="font-size:10pt;width:35px" onclick="left(true)"><br>
				                <input type="button" value=">" style="font-size:10pt;width:35px" onclick="right()"><br>
				                <input type="button" value=">>" style="font-size:10pt;width:35px" onclick="right(true)"><br>
			                </td>
			                <td>
			                	<fieldset>
			                		<legend>系統人員</legend>
				                	<select name="right_select" multiple="multiple" style="width : 152px" size="10" id="right_select">
				                		<option value="zhangsan">zhangsan</option>
				                		<option value="lisi">lisi</option>
				                		<option value="lisi">lisi</option>
				                		<option value="wangwu">wangwu</option>
				                	</select>
			                	</fieldset>
			                </td>
			            </tr>
			        </table>
			        </td>
		         </tr>
		     </table>

介面如下圖

首先jquery第一種方法:

[javascript] view plain copy print?
  1. //isAll 是否全部移動
  2. function left(isAll){  
  3.     var os = new Array();  
  4.     os = $("#right_select").find("option");  
  5.     for(i=0;i<os.length;i++){  
  6.         if(isAll){  
  7.             var o = new Option(os[i].text,os[i].value);  
  8.             $("#left_select").append(o);  
  9.             $("#right_select").find("option").remove();  
  10.             // == $("#right_select").empty();
  11.         }else{  
  12.             if(os[i].selected){  
  13.                 var o = new Option(os[i].text,os[i].value);  
  14.                 $("#left_select").append(o);  
  15.                 $("#right_select").find("option:selected").remove();  
  16.             }  
  17.         }  
  18.     }  
  19. }  
  20. function right(isAll){  
  21.     var os = new Array();  
  22.     os = $("#left_select").find("option");  
  23.     for(i=0;i<os.length;i++){  
  24.         if(isAll){  
  25.             var o = new Option(os[i].text,os[i].value);  
  26.             $("#right_select").append(o);  
  27.             $("#left_select").find("option").remove();  
  28.             // == $("#left_select").empty();
  29.         }else{  
  30.             if(os[i].selected){  
  31.                 var o = new Option(os[i].text,os[i].value);  
  32.                 $("#right_select").append(o);  
  33.                 $("#left_select").find("option:selected").remove();  
  34.             }  
  35.         }  
  36.     }  
  37. }  
//isAll 是否全部移動
function left(isAll){
	var os = new Array();
	os = $("#right_select").find("option");
	for(i=0;i<os.length;i++){
		if(isAll){
			var o = new Option(os[i].text,os[i].value);
			$("#left_select").append(o);
			$("#right_select").find("option").remove();
			// == $("#right_select").empty();
		}else{
			if(os[i].selected){
				var o = new Option(os[i].text,os[i].value);
				$("#left_select").append(o);
				$("#right_select").find("option:selected").remove();
			}
		}
	}
}
function right(isAll){
	var os = new Array();
	os = $("#left_select").find("option");
	for(i=0;i<os.length;i++){
		if(isAll){
			var o = new Option(os[i].text,os[i].value);
			$("#right_select").append(o);
			$("#left_select").find("option").remove();
			// == $("#left_select").empty();
		}else{
			if(os[i].selected){
				var o = new Option(os[i].text,os[i].value);
				$("#right_select").append(o);
				$("#left_select").find("option:selected").remove();
			}
		}
	}
}

第二種方法: [javascript] view plain copy print?
  1. /** 
  2.  * 此方法 移動的時候會自動刪除 不用手動去 remove 
  3.  * 但移走的選項會預設選中 無法取消,程式碼雖少,效果但不如第一種 
  4.  */
  5. function left(isAll){  
  6.     var os = new Array();  
  7.     os = $("#right_select").find("option");  
  8.     for(i=0;i<os.length;i++){  
  9.         if(isAll){  
  10.             $("#left_select").append(os[i]);  
  11.         }else{  
  12.             if(os[i].selected){  
  13.                 $("#left_select").append(os[i]);  
  14.             }  
  15.         }  
  16.     }  
  17. }  
  18. function right(isAll){  
  19.     var os = new Array();  
  20.     os = $("#left_select").find("option");  
  21.     for(i=0;i<os.length;i++){  
  22.         if(isAll){  
  23.             $("#right_select").append(os[i]);  
  24.         }else{  
  25.             if(os[i].selected){  
  26.                 $("#right_select").append(os[i]);  
  27.             }  
  28.         }  
  29.     }  
  30. }  
/**
 * 此方法 移動的時候會自動刪除 不用手動去 remove
 * 但移走的選項會預設選中 無法取消,程式碼雖少,效果但不如第一種
 */
function left(isAll){
	var os = new Array();
	os = $("#right_select").find("option");
	for(i=0;i<os.length;i++){
		if(isAll){
			$("#left_select").append(os[i]);
		}else{
			if(os[i].selected){
				$("#left_select").append(os[i]);
			}
		}
	}
}
function right(isAll){
	var os = new Array();
	os = $("#left_select").find("option");
	for(i=0;i<os.length;i++){
		if(isAll){
			$("#right_select").append(os[i]);
		}else{
			if(os[i].selected){
				$("#right_select").append(os[i]);
			}
		}
	}
}

JS實現如下:

[javascript] view plain copy print?
  1. function left(isAll)  
  2. {  
  3.     var os=new Array();  
  4.     os=document.getElementById("right_select").options;  
  5.     for(i=0;i<os.length;i++){  
  6.         if(isAll){  
  7.             var o=new Option(os[i].text,os[i].value);  
  8.             document.getElementById("left_select").add(o);  
  9.             document.getElementById("right_select").remove(i);  
  10.             i--;  
  11.         }else{  
  12.             if(os[i].selected){  
  13.                 var o=new Option(os[i].text,os[i].value);  
  14.                 document.getElementById("left_select").add(o);  
  15.                 document.getElementById("right_select").remove(i);  
  16.                 i--;  
  17.             }  
  18.         }  
  19.     }  
  20. }  
  21. function right(isAll)  
  22. {  
  23.     var os=new Array();  
  24.     os=document.getElementById("left_select").options;  
  25.     for(i=0;i<os.length;i++){  
  26.         if(isAll){  
  27.             var o=new Option(os[i].text,os[i].value);  
  28.             document.getElementById("right_select").add(o);  
  29.             document.getElementById("left_select").remove(i);  
  30.             i--;  
  31.         }else{  
  32.             if(os[i].selected){  
  33.                 var o=new Option(os[i].text,os[i].value);  
  34.                 document.getElementById("right_select").add(o);  
  35.                 document.getElementById("left_select").remove(i);  
  36.                 i--;  
  37.             }  
  38.         }  
  39.     }  
  40. }