简体   繁体   中英

populate 2nd listbox from 1st

I am trying various codes to populate a 2nd listbox (#box2) but to no avail. My latest attempt is that the JS is not showing anything on change so there must be an error in my code, but I cannot see it. I would be grateful if someone could check my code and show me where I have gone wrong. Many thanks

<?php

<select name="box1[]" id="box1" size="7" multiple="multiple" />

<?php
$i = 0;

while ($row = mysql_fetch_array($result))
    {
?>
<option value="<?php echo $row["custref"]; ?>">
 <?php echo $row["custref"]; ?></option>
<?php
    $i++;
    }

?>
<select name="box2[]" id="box2" size="7" multiple="multiple" />

<script type="text/javascript">


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

        var option = $(this).find('option:selected').val();

        $('#box2').val(option);

    });

    </script>

try this code it will append the selected option from Box1 to Box2

<script type="text/javascript">

    $('#box1').change(function()
    {
     $box1_value=$("#box1").val();
     $box1_text=$("#box1 option:selected").text();
     $("#box2").append('<option value="'+$box1_value+'">'+$box1_text+'</option>');
}
</script>

Find selected, clone, append:

$('#box1').change(function() {
  var option = $(this).find('option:selected').clone();
  $('#box2').append(option);
});

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