简体   繁体   English

使用ajax的值填充下拉列表

[英]Populate drop down with values from ajax

I have a issue where I can not populate drop down list with values from ajax. 我有一个问题,我无法使用ajax的值填充下拉列表。 In my console.log everything seems to be ok, but can not figure why my drop down list doesn't take values. 在我的console.log中,一切似乎都还可以,但是无法弄清楚为什么我的下拉列表没有值。

My view: 我的观点:

<div class="form-group">
  <label class="control-label col-lg-12">Type <span class="text-danger">*</span></label>
  <div class="col-lg-12">
    <select rows="5" cols="5" id="productcategory" class="form-control productcategory" required="required" placeholder="Default textarea">
      <option value="">Select Type</option>
      <option value="business">Business</option>
      <option value="branch">Branch</option>
      <option value="offer">Offer</option>
    </select>
  </div>
</div>

<div class="form-group">
  <label class="control-label col-lg-12">Type <span class="text-danger">*</span></label>
  <div class="col-lg-12">
    <select class="form-control name">
      <option value="">Product Name</option>
    </select>
  </div>
</div>

This is my script 这是我的剧本

<script type="text/javascript">
$(document).ready(function () {

  $(document).on('change', '.productcategory', function () {
    var cat_id = $(this).val();
    var div = $(this).parent();
    var op = " ";

    $.ajax({
      type:'get',
      url: '{!!URL::to('findProductName')!!}',
      data: { 'id': cat_id },
      success: function (data) {
        op += '<option value="0" selected disabled>chose product</option>';
        for (var i=0; i < data.length; i++) {
          console.log(data[i].id);
          console.log(data[i].name);

          op += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
        }

        div.find('.name').html(" ");
        div.find('.name').append(op);
      },
      error: function () {}
    });
  });
});
</script>

I repeat that in my console it works fine. 我在控制台中重复一遍,效果很好。

console.log(data[i].id);
console.log(data[i].name);

You call 你打电话

var div=$(this).parent();

which evaluates to: 结果为:

<div class="col-lg-12">

you then call 然后你打电话

div.find('.name')

which evaluates to: 结果为:

null/undefined

because the div .name is not inside the div your searching in. 因为div .name不在您搜索的div内。

Try adding an additional wrapper div around all your HTML Code and select the correct element: 尝试在所有HTML代码周围添加一个附加的包装div,然后选择正确的元素:

var div=$(this).parent().parent().parent(); // this is pretty bad because if you change HTML its instantly broken.

or 要么

var div = $('.yourNewDiv'); // this is better because its more readable, and your code isn't broke if you add a div inbetween f.e.

Example: https://jsfiddle.net/g8sua2Lt/1/ 示例: https//jsfiddle.net/g8sua2Lt/1/

Make sure you have selected "div" tag correctly as you have mentioned you are getting data in the console, so this might be one reason that dropdown is not populating. 正如您提到的那样,请确保已正确选择“ div”标签,因为您正在控制台中获取数据,因此这可能是下拉列表未填充的原因之一。 Or you can try the following code: 或者,您可以尝试以下代码:

$.ajax({
        type:'get',
        url:'{!!URL::to('findProductName')!!}',
        data:{'id':cat_id},
        success:function(data){
            op+='<select class="form-control name">';
            op+='<option value="0" selected disabled>chose product</option>';
            for(var i=0;i<data.length;i++){
                console.log(data[i].id);
                console.log(data[i].name);

                op+='<option value="'+data[i].id+'">'+data[i].name+'</option>';
            }

            op+='</select>'
            div.find('.name').html(op);                
        },
        error:function(){

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM