jQuery moving MultiSelect values to another MultiSelect

前端 未结 8 1275
感情败类
感情败类 2020-12-23 12:27

So I have a MultiSelect box with x values which I need the ability to move to another MultiSelect box and vise versa.


                        
    
提交评论

  • 2020-12-23 13:17

    Here goes my html code

    <div>
            <select multiple id="select1">
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
                <option value="4">Option 4</option>
                <option value="5">Option 5</option>
                <option value="6">Option 6</option>
            </select>
            <a href="#" id="add">add &gt; &gt;</a>
        </div>
        <div>
            <select multiple id="select2"></select>
            <a href="#" id="remove">remove &lt; &lt;</a>
        </div>
    

    and Javascript code

    $("#add").click(function(){
        $("#select1 option:selected").remove().appendTo($("#select2"));
    })
    $("#remove").click(function(){
        $("#select2 option:selected").remove().appendTo($("#select1"));
    })
    

    Make sure you imported jquery.js file.

    0 讨论(0)
  • 提交回复
    热议问题