获取ajax数据并在引导模态中显示 [英] get ajax data and display on bootstrap modal

查看:80
本文介绍了获取ajax数据并在引导模态中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个托管餐厅菜单的应用程序.单击菜单项时,我想通过Ajax从我的api中获取该项目的数据,并将其显示在引导程序模式中.

Im building an app the hosts restaurant menus. When a menu item is clicked i want to fetch the item's data from my api via ajax and display it on a bootstrap modal.

javascript

javascript

(function() {
var infoModal = $('#myModal');
$('.modal-toggle').on('click', function(){
    $.ajax({
        type: "GET",
        url: '/api/menu-item/'+$(this).data('id'),
        dataType: 'json',
        success: function(data){
            var item = JSON.parse(data);
            var htmlData = '<ul><li>';
            htmlData += item.name;
            htmlData += '</li></ul>';
            infoModal.find('#modal-body').innerHTML = htmlData;
        }
    });
    infoModal.modal();
});
})(jQuery);

模式触发器

<a class="modal-toggle" data-toggle="modal" data-target="#myModal" data-id="{{{ $item->id }}}"><h5>{{{ $item->name }}}</h5></a>

模态

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-      labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="exampleModalLabel">New message</h4>
  </div>
  <div class="modal-body" id="modal-body"></div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary">Add to cart</button>
  </div>
</div>

apis响应

{"id":4,"menu_category_id":446,"name":"kunzereichert","description":"Dolores impedit ut doloribus et a et aut.","price":"999.99","created_at":"2015-04-10 05:55:23","updated_at":"2015-04-10 05:55:23"}

当我单击触发器时,会弹出模态,并且ajax请求以200的状态代码出现在我的浏览器网络上,但是模态内部html不会更新.我知道这里可能存在这些问题,但是我似乎找不到一个有帮助的问题.请帮助

when i click on the trigger the modal pops up and the ajax request appears on my browsers networks with a status code of 200, however the modals inner html is not updated. I know theres probably docens of these questions here, but i couldnt seem to find one that helped. please help

推荐答案

代替此:

infoModal.find('#modal-body').innerHTML = htmlData;

您应该使用此:

infoModal.find('#modal-body')[0].innerHTML = htmlData;

如果您要坚持使用全jQuery方式,请使用以下方法:

If you want to stick to all-jQuery way, then use this:

infoModal.find('#modal-body').html(htmlData);

这里是JSFiddle,使用的是假回复:

Here's the JSFiddle, using a fake response:

https://jsfiddle.net/6o7atqd3/1/

这篇关于获取ajax数据并在引导模态中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆