2012-07-10 47 views
4

我有两个组合框,一个让所有可用的国家名称,另一个是空的,如:的jQuery移动项目根据组合框选择

国家

<select name="countryId[]" multiple="multiple" id="countryId" size="10"> 
    <option id="" value="1" >Afghanistan</option> 
    <option id="" value="3" >Albania</option> 
    <option id="" value="4" >Algeria</option> 
    <option id="" value="5" >American samoa</option> 
</select> 

<select name="mycountryId[]" multiple="multiple" id="mycountryId" size="10"> 
</select> 

现在,如果我从国家列表中选择任何国家,它应该移动到空列表但选择属性和详细,如果我选择的国家名单结果三个国家的一些事情应该是:

国家

<select name="countryId[]" multiple="multiple" id="countryId" size="10"> 
    <option id="" value="1" >Afghanistan</option> 
</select> 

<select name="mycountryId[]" multiple="multiple" id="mycountryId" size="10"> 
    <option selected="selected" id="" value="3" >Albania</option> 
    <option selected="selected" id="" value="4" >Algeria</option> 
    <option selected="selected" id="" value="5" >American samoa</option> 
</select> 

和类似的,如果我点击来自我的新国家的任何国家列表(命名为),那么所选择的国家应该搬回提供国家未选中的属性和详细值列表国家 阿富汗 阿尔及利亚

我跟随Jquery - Moving Items from one list to another based on the combobox selection它是非常好的一段代码,但如何使所选的所有值在targetCars中移动?选择属性和反之亦然?

回答

1

首先,为什么你在控制ID中使用[]?你可以在select元素Manipulating Select options

+0

感谢朋友:) – 2012-07-10 08:53:01

+0

感谢Furqan我只是用6替换了我的jQuery。将列表中的选项从列表A移动到列表B,它像一个向导一样工作,谢谢兄弟.... :)投票4 U – 2012-07-14 11:06:54

10

enter image description here

function MoveListBoxItem(leftListBoxID, rightListBoxID, isMoveAll) { 
    if (isMoveAll == true) { 

     $('#' + leftListBoxID + ' option').remove().appendTo('#' + rightListBoxID).removeAttr('selected'); 

    } 
    else { 

     $('#' + leftListBoxID + ' option:selected').remove().appendTo('#' + rightListBoxID).removeAttr('selected'); 
    } 
} 
+2

您的代码应该有是正确的答案,简单得多。 – 2014-06-03 21:25:53

1

这里去html代码移动从一个列表中的项目到另一个使用此,

 $('#countryId option:selected').appendTo('#countryId'); 

更多的操作!

 <select multiple id="countryId"> 
      <option value="1">Afghanistan</option> 
      <option value="2">Albania</option> 
      <option value="3">Algeria</option> 
      <option value="4">American samoa</option> 
     </select> 
     <a href="#" id="add">add &gt; &gt;</a> 

     <select multiple id="mycountryId"></select> 
     <a href="#" id="remove">remove &lt; &lt;</a> 

和我的JavaScript代码。

$("#countryId").click(function(){ 
    $("#countryId option:selected").remove().appendTo($("#mycountryId")); 
}) 
$("#mycountryId").click(function(){ 
    $("#mycountryId option:selected").remove().appendTo($("#countryId")); 
})  

确保您已将jquery.js文件添加到您的项目!

相关问题