最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to create dropdown menu onchange on another dropdown list - Stack Overflow

matteradmin7PV0评论

Can anyone give me demo on how to create dropdown dynamically onchange on another dropdown

I have one dropdown menu in which one can select multiple option like this

  <select id='first' multiple='multiple'>
      <option value='1'>1</option>
      <option value='2'>2</option>
      <option value='3'>3</option>
      <option value='4'>4</option>
      <option value='5'>5</option>
      <option value='6'>6</option>
      <option value='7'>7</option>
      <option value='8'>8</option>
  </select>

suppose if user selected first three option from above 1,2 and 3, I am interested to create one more dropdown with single selection like this in one division

<div id='above_selected'>
    <select id='first_selected'>
      <option value='1'>1</option>
      <option value='2'>2</option>
      <option value='3'>3</option>
    </select>
</div>

As and when there is a change on id='first' I would like to reflect it on another division

Please someone help me.

Can anyone give me demo on how to create dropdown dynamically onchange on another dropdown

I have one dropdown menu in which one can select multiple option like this

  <select id='first' multiple='multiple'>
      <option value='1'>1</option>
      <option value='2'>2</option>
      <option value='3'>3</option>
      <option value='4'>4</option>
      <option value='5'>5</option>
      <option value='6'>6</option>
      <option value='7'>7</option>
      <option value='8'>8</option>
  </select>

suppose if user selected first three option from above 1,2 and 3, I am interested to create one more dropdown with single selection like this in one division

<div id='above_selected'>
    <select id='first_selected'>
      <option value='1'>1</option>
      <option value='2'>2</option>
      <option value='3'>3</option>
    </select>
</div>

As and when there is a change on id='first' I would like to reflect it on another division

Please someone help me.

Share Improve this question asked Apr 2, 2014 at 16:16 user3304642user3304642 1911 gold badge2 silver badges16 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

Use:

$('#first').change(function () {
    $('#above_selected').html("<select id='first_selected'></select>");
    for (var i = 0; i < $(this).val().length; i++) {
        $('#first_selected').append('<option value="' + $(this).val()[i] + '">' + $(this).val()[i] + '</option>');
    }
})

jsFiddle example

Check this is demo jsFiddle

jQUery

$(document).ready(function() {
    var dropdown1 = {
        1 : ['Four', 'Five', 'Six'],
        2 : ['Seven', 'Eight', 'Nine'],
        3 : ['Ten', 'Eleven', 'Twelve'],
        4 : ['Thirteen', 'Fourteen', 'Fifteen'],
        5 : ['Sixteen', 'Seventeen', 'Eighteen']            
    }
    $('#first_selected').html(
            '<option>'+dropdown1[1].join('</option><option>')+'</option>'
        );
    $('#first').on('change',function() {
        $('#first_selected').html(
            '<option>'+dropdown1[$(this).val()].join('</option><option>')+'</option>'
        );
    });
});

HTML

<select id="first">
      <option value='1'>1</option>
      <option value='2'>2</option>
      <option value='3'>3</option>
      <option value='4'>4</option>
      <option value='5'>5</option>
</select>
<select id="first_selected">

I think this way is helpful for you to create JavaScript array base on selection array item bind to the another drop down list. Hope this help you!

Here is a very basic demo.

http://jsfiddle/tLj5z/1/

You will need to update this for your own purposes, but this should give you an idea of where to start.

$('#first').change(function(){
       var selected = $(this).find("option:selected");
        $("#first_selected").append(selected);
});

$('#first_selected').change(function(){
       var selected = $(this).find("option:selected");
        $("#first").append(selected);
});

You can do this:

Add <div class="add"></div> and then do the below

 $('#first').change(function () {
    if (Number($(this).val()) < 4) {
       $('div.add').html("<div id='above_selected'>    <select id='first_selected'>      <option value='1'>1</option><option value='2'>2</option>      <option value='3'>3</option></select></div>");
    }
});

Demo: http://jsfiddle/AmitJoki/6KfBk/1

Post a comment

comment list (0)

  1. No comments so far