2012-07-10 46 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文件添加到您的項目!

相關問題