jQuery实现Select多选列表双击选中项时相互添加

来源:互联网 发布:pop3的端口号 编辑:程序博客网 时间:2024/05/19 23:17

jQuery实现在左边双击某一项时添加到右边,右边时添加到左边:


部分修饰代码简略:

Java代码 复制代码 收藏代码
  1. <table style="...">   
  2.                             <tr>   
  3.                                 <td colspan="2" valign="top" width="47%">    
  4.                                     <select multiple="multiple"  id="uncountryTable" name="uncountryTable" style="height:300px;width:245px;">                                        
  5.                                            
  6.                                     </select>   
  7.                                 </td>   
  8.                                 <td align="center">   
  9.                                     ...    
  10.                                 </td>   
  11.                                 <td colspan="2" valign="top" width="47%">   
  12.                                     <select multiple="multiple" id="countryTable" name="countryTable" style="height:300px;width:245px;">   
  13.                                                                        
  14.                                     </select>   
  15.                                 </td>   
  16.                             </tr>   
  17.                             </table>  

 以左边为例:

刚开始我的思路是这样的: 当左边select变化时,如果有一项选中,那么当它被双击时就执行相应的操作;但当实施时才发现这样根本不可行,代码如下:

Java代码 复制代码 收藏代码
  1. //jQuery("#uncountryTable").change(function(){   
  2.                 //jQuery("#uncountryTable option").each(function(){  
  3.                     //if(jQuery(this).attr("selected") == true){                      
  4.                         //jQuery(this).dblclick(function(){  
  5.                             //alert(111111);  
  6.                             //var option = "<option value='"+jQuery(this).val()+"'>"+jQuery(this).text()+"</option>";                     
  7.                             //jQuery("#countryTable").append(option);  
  8.                             //jQuery(this).remove();  
  9.                         //});   
  10.                     //}   
  11.                 //});   
  12.             //});  

 其实一直到判断选中的这一步都是正确的,经测试此时也确实可以alert出相应选中项的值

(多选下拉框获取选中文本、value:  也可以用change方法)

Java代码 复制代码 收藏代码
  1. //jQuery("#uncountryTable").change(function(){   
  2.                 //jQuery("#uncountryTable option").each(function(){  
  3.                     //if(jQuery(this).attr("selected") == true){  
  4.                         //alert(jQuery(this).val()+":"+jQuery(this).text());  
  5.                     //}   
  6.                 //});   
  7.             //});  

 但是一到dblclick(fn)方法这步就有问题了,连测试的111111都不弹出,几经测试都无效果,无奈只得另谋它路。


在网上搜了一下,发现一个哥们用JS写的同样效果代码(下面),看了之后才发现原来自己想得太复杂了,正确思路应该是这样的: 当双击select的时候,触发dblclick(fn)事件,如果某一项被选中(你双击某一项,它就肯定被选中啦),就将其添加到右边,这样就可以实现效果,哈哈。

上代码:

Java代码 复制代码 收藏代码
  1. jQuery(document).ready(function() {        
  2. jQuery("#uncountryTable").dblclick(function(){   
  3.                 jQuery("#uncountryTable option:selected").each(function(){   
  4.                     var option = "<option value='"+jQuery(this).val()+"'>"+jQuery(this).text()+"</option>";                    
  5.                     jQuery("#countryTable").append(option);   
  6.                     jQuery(this).remove();   
  7.                 });   
  8.             });    
  9. });  

 很简单的东西,换下思路就实现了,因此,以后遇到问题时,就先问下自己,是不是我想得太复杂了,哈哈......


那哥们效果+代码(主要部分):


Java代码 复制代码 收藏代码
  1. <head>   
  2.     <script>   
  3.         function removeItem(){   
  4.             var sltSrc=document.getElementById('sltSrc');   
  5.             var sltTarget=document.getElementById('sltTarget');   
  6.             for(var i=0;i<sltSrc.options.length;i++)   
  7.             {   
  8.                 var tempOption=sltSrc.options[i];   
  9.                 if(tempOption.selected){   
  10.                     sltSrc.removeChild(tempOption);   
  11.                     sltTarget.appendChild(tempOption);   
  12.                 }       
  13.             }   
  14.         }           
  15.     </script>   
  16. </head>   
  17. <body>   
  18.     <select ondblclick="removeItem();" id="sltSrc" multiple="true">   
  19.         <option value="a">srcA</option>   
  20.         <option value="b">srcB</option>   
  21.         <option value="c">srcC</option>   
  22.     </select>   
  23.     <select ondblclick="addItem();" id="sltTarget"  multiple="true">   
  24.         <option value="a">targetA</option>   
  25.         <option value="b">targetB</option>   
  26.         <option value="c">targetC</option>   
  27.     </select>   
  28.     <div id="showInfo"></div>   
  29.     <input type="button" value="showSelectOptions"  onclick="showSelectOptions();"/>   
  30. </body>  

 

原创粉丝点击