Use jQuery to change a second select list based on the first select list option

后端 未结 8 2105
你的背包
你的背包 2020-11-22 14:34

I have two selects:


                        
    
提交评论

  • 2020-11-22 15:08

    Store all #select2's options in a variable, filter them according to the value of the chosen option in #select1, and set them using .html() in #select2:

    var $select1 = $( '#select1' ),
        $select2 = $( '#select2' ),
        $options = $select2.find( 'option' );
    
    $select1.on('change', function() {
        $select2.html($options.filter('[value="' + this.value + '"]'));
    }).trigger('change'); 
    

    Here's a fiddle

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