douhuangjian9627 2017-12-19 12:44
浏览 60

显示基于主类别的子类别不适用于AJAX

I have a car "make" and car "model" tables in my database. When I echo car "makes" it works properly. However, I want the car "model" drop-down to appear with ajax based on selected "make", it does not work in the view

enter image description here

However, all information that I need, appears on the console's network tab.

enter image description here

Where can be the problem? Here is my ajax code:

<script type="text/javascript">
var base_url = "<?php echo base_url();?>";

    $(document).ready(function() {
        $('select[name="make"]').on('change', function() {
            var makeid = $(this).val();
            if(makeid) {
                $.ajax({
                    url: base_url + 'myform/ajax/'+makeid,
                    type: "GET",
                    dataType: "json",
                    success:function(data) {
                        $('select[name="model"]').empty();
                        $.each(data, function(key, value) {
                            $('select[name="model"]').append('<option value="value.id">'+ 'value.title' +'</option>');
                        });
                    }
                });
            }else{
                $('select[name="model"]').empty();
            }
        });
    });
</script>

Here is the view:

<?php $attributes = array( 'name' => 'addcar', 'id' => 'addcar'); ?>
                  <?php echo form_open_multipart('cars/create', $attributes); ?>

                            <div class="form-group label-floating">
                                <label class="control-label">
                                    Марка
                                    <small>*</small>
                                </label>
                                <select id="make" name="make" class="selectpicker" data-style="select-with-transition" required="true">
                                    <option value=""></option>

                                    <?php foreach($makes as $make): ?>
                                    <option value="<?= $make['id']; ?>"><?= $make['maketitle']; ?></option>
                                <?php endforeach; ?>
                              </select>
                            </div>

                            <div class="form-group label-floating">
                                <label class="control-label">
                                    Модель
                                    <small>*</small>
                                </label>
                                <select id="model" name="model" class="selectpicker" data-style="select-with-transition" required="true">

                              </select>
                            </div>

</form>
  • 写回答

3条回答 默认 最新

  • dongqiang1894 2017-12-19 12:56
    关注

    You're not writing your values correctly, and you need to match the properties from the objects in your response to what you're trying to output:

    $('select[name="model"]').append('<option value="value.id">'+ 'value.title' +'</option>');
    

    should be

    $('select[name="model"]').append('<option value="' + value.id + '">'+ value.modeltitle +'</option>');
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?