How to copy

后端 未结 4 1980
忘掉有多难
忘掉有多难 2020-12-14 05:39

I am using the two sided multi select found here http://www.stevefenton.co.uk/cmsfiles/assets/File/twosidedmultiselect.html and need to add the selected options in the right

相关标签:
4条回答
  • 2020-12-14 05:57
    var $options = $("#myselect > option").clone();
    
    $('#secondSelectId').append($options);
    

    Live DEMO

    0 讨论(0)
  • 2020-12-14 06:00
    <select multiple="true" class="multiselect1" name="myselecttsms1">
        <option value="1" rel="0" title="One">One</option>
        <option value="2" rel="1" title="Two">Two</option>            
        <option value="4" rel="3" title="Four">Four</option>
        <option value="5" rel="4" title="Five">Five</option>
        <option value="6" rel="5" title="Six">Six</option>
    </select>
    
    <select multiple="true" class="multiselect2" name="myselecttsms2" size="6">
    
    </select>
    
    <button class="add">Add</button>
    <button class="addAll">Add All</button>
    <button class="remove">Remove</button>
    <button class="removeAll">Remove All</button>
    

    jQuery:

    $('.add').on('click', function() {
        var options = $('select.multiselect1 option:selected').sort().clone();
        $('select.multiselect2').append(options);
    });
    $('.addAll').on('click', function() {
        var options = $('select.multiselect1 option').sort().clone();
        $('select.multiselect2').append(options);
    });
    $('.remove').on('click', function() {
        $('select.multiselect2 option:selected').remove();
    });
    $('.removeAll').on('click', function() {
        $('select.multiselect2').empty();
    });
    

    Sample Workout

    0 讨论(0)
  • 2020-12-14 06:05
    $('#cloneBtn').click(function() {
        var $options = $("#myselect > option").clone();
        $('#second').empty();
        $('#second').append($options);
        $('#second').val($('#myselect').val());
    });
    

    This is used to copy the value and the innerHTML. Its better to copy the key, value and the OPTIONS.i.e. the selected value and the options.

    0 讨论(0)
  • 2020-12-14 06:09

    There is an even easier way to do this:

    var options = $("#myOldSelect").html();
    $('#myNewSelect').html(options);
    

    This is really handy if you want to add the new Select to the DOM with the copied options already included:

    var options = $("#myOldSelect").html();
    $("#domLocation").append("<select id='myNewSelect'>" + options + "</select");
    
    0 讨论(0)
提交回复
热议问题