简体   繁体   中英

jQuery copy selectbox selected value to another selectbox

Is it possible to copy a selected option from the first dropdown to another using Jquery and disallowing the user from copying the SAME option ?

在此处输入图片说明

HTML Markup

<select id="one" multiple="multiple">
    <option>one</option>
    <option>two</option>
    <option>three</option>
</select>

<select id="two" multiple="multiple">

</select>

Jquery

$('#one option').click(function() {
   $(this).clone().appendTo("#two");
});

FIDDLE

Yes:

var option = $("#idOfSelect option:eq(0)"); //eq 0 targets the first option, change to what you need
$("#idOfOtherSelect").append(option);

Like this:

$('#first').change(function(){

    $('#second').append("<option value="+$(this).val()+">"+$("#first option:selected").text()+"</option>")

})

FIDDLE EXAMPLE

Example:

$("#copy").on("click", function(e) {
    e.preventDefault();
    $("#select1 option:selected").clone().appendTo($("#select2"));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM