如何将Ajax响应显示为模式弹出窗口

时间:2016-06-24 06:56:27

标签: javascript java jquery html ajax

我有一个关于点击它的链接正在发送ajax请求并成功获得响应,这是html文件,我将附加到div,但我需要将div显示为modal popup我试过以下内容。

<{1>}文件中的

html
<{1>}文件中的

<a th:if="${ratingSummary}" href="#"  class="small dark account review_ratings_login">Login to write a review</a> 

<div id="login_for_review" data-toggle="modal" data-target="#reviewLoginModal"></div>

js档案

$(document).on('click', '.review_ratings_login', function () {
        var $data = $('#review_product_id span').text();
         var url = '/mycompany/login/'+$data;
        $.ajax({
            type: 'GET',
            url: url,
            success: function (output) {
            $('#login_for_review').html(output).modal('show');// I tried to show this response as modal popup
            },
            error: function(output){
            alert("fail");
            }
        });
    });

但是我没有将此output作为<div class="centerForm modal fade" role="dialog" style="margin-left: 35%;" id="reviewLoginModal"> <div class="modal-dialog modal-sm" > <div class="modal-content"> // here I have login form </div> </div> ,而是我得到html output任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:4)

在Bootsrap模式弹出窗口中,您可以使用简单的方式来显示不需要预定义模态div容器的模态。见modal

对于E.g

 $.ajax({
                url: "url",
                type: 'POST',
                dataType: "html",
                data:{id:params},
                success: function(data, status, xhr) {
                    if(data==""){
                        window.location.href="/";
                    }
                    else{
                        BootstrapDialog.show({
                            title: "Modal Tital",
                            message: function(dialogRef){
                                $mydata = $($.parseHTML(data));
                                return $mydata;
                            },
                            onshow: function(dialog){

                        // and css change if need, eg. 
                         dialog.$modalHeader.css("float","none");

                            },
                            onshown:function(dialog)
                            {
                               // event after shown

                            },
                            onhide:function(dailog)
                            {
                               // event on hide
                            }
                        });
                    }

                },
                statusCode: {
                    401: function () {
                        alert("Your session has been expired");

                    }
                }
            });

答案 1 :(得分:0)

我通过创建模态并删除data-toggledata-target并仅添加对modal div

的响应来解决此问题

代码对于模态div

<div id="login_for_review" class="modal hide"  role="dialog">

</div>

代码用于超链接

 <a th:if="${ratingSummary}" href="#"  class="small dark account review_ratings_login">Login to write a review</a>

ajax调用代码

$(document).on('click', '.review_ratings_login', function () {
        var $data = $('#review_product_id span').text();
         var url = '/mycompany/login/'+$data;
        $.ajax({
            type: 'GET',
            url: url,
            success: function (output) {
            $('#login_for_review').html(output).modal('show');//now its working
            },
            error: function(output){
            alert("fail");
            }
        });
    });