2017-02-26 16 views
0

如何在dropdownbox中使用jquery追加值?如何在dropdownbox中追加值

這裏是我的html

<div class="input-field col s6"> 
    <select id="graph_program" class="Programs"> 
    </select> 
    <label>Program</label> 
</div> 

這裏是我的腳本

$('#graph_college').on('change',function(){ 
     var college = $('#graph_college option:selected').text(); 
     $.ajax({ 
      type:"POST", 
      url:"<?php echo base_url('administrator/getCourses')?>", 
      data:{college:college}, 
      dataType:'json', 
      success : function(data){ 
       console.log(data); 
       $(data).each(function(){ 
        $('.Programs').append($('<option class="black-text"></option>').text(this.course_abbv)); 
       }); 
      } 
     }); 
    }); 

這裏是結果:(項目都在下拉框的底部)

+0

您希望物品出現在哪裏,如果不在底部?這是標準的行爲,如果你想要別的東西,你將不得不使用自定義下拉元素。 –

+0

我希望他們在下拉框內出現我新使用jquery –

+0

那麼,你有的代碼是好的,請參閱[this](https://jsfiddle.net/xzjuq04o/)。別的東西正在造成你所看到的,我們不可能知道什麼,因爲它不在你發佈的代碼中。 –

回答

0
ent$('#graph_college').on('change',function(){ 
    var college = $('#graph_college option:selected').text(); 
    $.ajax({ 
     type:"POST", 
     url:"<?php echo base_url('administrator/getCourses')?>", 
     data:{college:college}, 
     dataType:'json', 
     success : function(data){ 
      console.log(data); 
      $(data).each(function(){ 
       $('.Programs').append($('<option class="black-text"></option>').text(this.course_abbv)); 
      }); 
      $('select').material_select(); // answer 
     } 
    }); 
}); 

我需要重新初始化物品的下拉框