如何在引导模式中显示ajax响应

时间:2017-11-14 07:01:48

标签: javascript php jquery ajax

我想弹出一个模态,它将显示来自查询的动态数据。所以当用户点击相关学校时会显示为模态。我得到了回应,但我没有得到我怎么能显示到那个模态。有什么建议吗?

按钮:

 <a href="#." data-toggle="modal" data-target="#myModal" data-id="{{$user->id}}"  class="chooseSchoolBtn">Choose School</a>

Ajax:

$(".chooseSchoolBtn").on("click", function (argument) {
    //console.log($(this).data("id"))
    var user_id = $(this).data("id")
    var info = $.get("{{url('school-list')}}", {
        id: user_id
    });
    info.done(function (data) {
        console.log(data)
    });
});

模态:

<div class="modal-body">
    // This section will come dynamically
    <div class="col-md-2 col-sm-4 col-xs-12">
        <div class="single_school">
            <div class="selection_area">
                <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
            </div>
            <div class="school_img_area" style="background-image: url(school/images/school/school1.jpg);">
            </div>
            <div class="school_info">
                <p class="school_name">School Name</p>
                <p class="school_description">Good School...</p>
                <p class="school_price">12$</p>
            </div>
        </div>
    </div>
    // dynamic section ends
</div>

json回复:

{
    id: 1,
    user_id: 2,
    price: "2340.00",
    name: "XYZ",
    display_image: "1510570005ajaPXajmik.png"
}, {
    id: 3,
    user_id: 2,
    price: "123.00",
    name: "ABC",
    display_image: "1510570049QZWiQUh7zY.jpg"
}

4 个答案:

答案 0 :(得分:1)

只需更改ajax响应数据

即可
info.done(function(data){          
    $.each(data,function(index,value){
        $(".school_img_area").attr("style","background-image: url(school/images/school/"+value.display_image+");");
        $(".school_name").html(value.name);
        $(".school_price").html(value.price);
    });
});

快乐编码: - )

答案 1 :(得分:0)

$(".chooseSchoolBtn").on("click", function (argument) {
      //console.log($(this).data("id"))
      var user_id = $(this).data("id")
      var info=$.get("{{url('school-list')}}",{id:user_id});
        info.done(function(data){          
              $('DIV WHERE YOU WNAT TO DISPLAY ID').html(data.id);
              // repeat same for user_id, price, so on;
      });
      });

答案 2 :(得分:0)

如果你想使用JSON响应实现,那么你将迭代循环并将其附加到html。

前 -

var html="";
for(var i=0; i<data.length; i++) {
    var html +=<p>data.price</p>data.name<p>;
}
$('.appendTo').html(html);

其他选项在您的控制器方法中,而不是将数据作为JSON返回,您可以返回HTML,而不是在获得ajax成功时替换内容。

答案 3 :(得分:0)

$(document).on("click", ".chooseSchoolBtn", function () {
        var schoolId= $(this).data('id');        
        $('#modalId').modal('show');
        $.ajax({
            type: "POST",
            url: "some_url_to_get_school_info",
            data: {'sid':schoolId},
            success: function(data){ 
              $('#div_id_to_show').html(data);
            },
            error: function(){
                alert("Fail")
            }
        });
    });