当我选择第一个时,Jquery Second Select框不会填充

时间:2017-02-04 03:37:30

标签: javascript jquery html

我遵循了ajax代码,该代码将在选择第一个选择框时触发

$('#select_modal_name').on('change',function(){
      $('#loader').show();
       var myselect='';
      var pageurl =  'http://localhost/demo/public/';
        $.ajax({
        url: pageurl+'/brand-name',
        type: 'GET',

        success: function(data){
             $('#loader').hide();

               $.each(data, function( key, value ) {
                   myselect+='<option value="'+value.id+'" >'+value.brand_name+'</option>';   


});

 $('#try-this').append(myselect);

console.log(myselect);

         }

});
});

html代码

<div class="form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12">Select Model</label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                            <select id="select_modal_name" class="selectpicker show-tick form-control" data-live-search="true">
          <option value="AK" selected>---Select--</option>
            @foreach($data as $key=>$val)
           <option value="{{$val->id}}">{{$val->brand_name}}</option>
           @endforeach;

        </select>
                        </div>
                      </div>
                     <div class="item form-group">
                        <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Model Parts Name <span class="required">*</span>
                        </label>
                        <div class="col-md-6 col-sm-6 col-xs-12">
                     <select id="basic select-parts-name" class="selectpicker show-tick form-control " data-live-search="true">

          <div id="try-this" class="try-this" ></div>
         <!--  <option value="AK" selected>---Select--</option>--->

        </select>
                        </div>

即使我已成功通知选项ajax数据的输出是打印。仅问题是它不附加到选择框

1 个答案:

答案 0 :(得分:0)

是的,最后解决了我的问题

问题是我使用selectpicker js来设计选择框。我使用类名附加了选项值并添加了selectpicker refresh

$('.selectpicker').append(myselect);
$('.selectpicker').selectpicker('refresh');

现在工作正常。