jQuery根据组合框选择将项目从一个列表移动到另一个列表

jQuery根据组合框选择将项目从一个列表移动到另一个列表,jquery,Jquery,我有两个组合框,一个包含所有可用国家的名称,另一个为空,如: 国家 空的 现在,如果我从国家/地区列表中选择任何国家/地区,它应移动到空列表,但具有选定属性和精确值。如果我从国家/地区列表中选择三个国家/地区,则结果应为: 国家 空的 类似地,如果我单击了名为Empty的新国家/地区列表中的任何国家/地区,则所选国家/地区应移回具有未选择属性和精确值的可用国家/地区列表 喜欢 国家 阿富汗 阿尔及利亚 我明白这是一段非常好的代码,但是如何选择在targetCars中移动的所有值呢?使用选定属性

我有两个组合框,一个包含所有可用国家的名称,另一个为空,如:

国家

空的

现在,如果我从国家/地区列表中选择任何国家/地区,它应移动到空列表,但具有选定属性和精确值。如果我从国家/地区列表中选择三个国家/地区,则结果应为:

国家

空的

类似地,如果我单击了名为Empty的新国家/地区列表中的任何国家/地区,则所选国家/地区应移回具有未选择属性和精确值的可用国家/地区列表 喜欢 国家 阿富汗 阿尔及利亚

我明白这是一段非常好的代码,但是如何选择在targetCars中移动的所有值呢?使用选定属性,反之亦然?

首先,为什么要在控件ID中使用[]?您可以使用以下命令将项目从一个列表移动到另一个列表:

      $('#countryId option:selected').appendTo('#countryId');
有关“选择图元”操作的详细信息

函数MoveListBoxItemleftListBoxID、rightListBoxID、isMoveAll{ 如果isMoveAll==true{ $+leftListBoxID+'option'.remove.appendTo+rightListBoxID.removeAttr'selected'; } 否则{ $+leftListBoxID+“选项:已选定”。remove.appendTo+rightListBoxID.removeAttr“已选定”; } }
下面是html代码

        <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>

确保已将jquery.js文件添加到项目中

谢谢Furqan,我刚刚用6替换了我的jQuery。将选项从列表A移动到列表B。它像向导一样工作感谢兄弟投票表决你的代码应该是正确的答案,简单得多。
<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>
      $('#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>
$("#countryId").click(function(){
    $("#countryId option:selected").remove().appendTo($("#mycountryId"));
})
$("#mycountryId").click(function(){
    $("#mycountryId option:selected").remove().appendTo($("#countryId"));
})