ajax自定义对话框成功

时间:2014-12-12 02:19:39

标签: javascript jquery html ajax

我已经编写了一个自定义对话框/模式框,如下所示:

<div id="somedialog" class="dialog">
    <div class="dialog__overlay"></div>
    <div class="dialog__content">
        <h2><strong>Howdy</strong>, I'm a dialog box</h2><
        div><button class="action" data-dialog-close>Close</button></div>
    </div>
</div>

目前由带有javascript代码的按钮触发,如下所示:

<script src="js/classie.js"></script>
<script src="js/dialogFx.js"></script>
<script>
    (function() {

        var dlgtrigger = document.querySelector( '[data-dialog]' ),
            somedialog = document.getElementById( dlgtrigger.getAttribute( 'data-dialog' ) ),
            dlg = new DialogFx( somedialog );

        dlgtrigger.addEventListener( 'click', dlg.toggle.bind(dlg) );

    })();
</script>

...我们的触发按钮具有数据属性data-dialog="somedialog"

但是,现在我只想在我的AJAX返回成功时调用此自定义对话框,并将来自AJAX的消息传递到对话框的<h2>文本中。我该怎么做呢?

ajax电话:

     <script>   
        $(document).on("click", "#submit", function(){
    var $self = $(this);
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    var request = $.ajax({
        url: "login.php",
        type: "POST",
        data: { username: username, password: password},
        beforeSend: function(){
            $self.html("Loading please wait...");
        }
    });

    //WHEN SUCCESS
    request.success(function( data ) {
       if( data == 'user' )
        {
             window.location.href =  "filter.php";
        } else if(data=='company'){
             window.location.href = "filter.php";

        }else {
            alert("Wrong Username or Password!");
             window.location.href = "indexwithlogin.php";

<insert the dialog box here>

        }
    });
});
</script>

2 个答案:

答案 0 :(得分:0)

从第一个代码块中,您使用以下行调用对话框:

dlgtrigger.addEventListener( 'click', dlg.toggle.bind(dlg) );

...所以只需要在从AJAX调用中获得成功/完成响应时调用它。我对您的脚本进行了一些更改:

  • 使用jqXHR延迟对象.done()代替.success()。后者已被弃用而不是.done(),而.error()则相同,后者由.fail()继承。
  • 将原生JS代码转换为jQuery以用于对话框变量声明,以保持一致性 - 尽管使用本机JS完全没问题,但这里缺乏一致性
  • 通过注释来删除窗口重定向,因为它违背了显示对话框的目的,不是吗?

这是修订后的JS:

$(document).on("click", "#submit", function(){
    var $self = $(this);
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    var request = $.ajax({
        url: "login.php",
        type: "POST",
        data: { username: username, password: password},
        beforeSend: function(){
            $self.html("Loading please wait...");
        }
    });

    // When successful
    // Use deferred object .done()
    request.done(function(data) {
        if(data=='user') {
            window.location.href = "filter.php";
        } else if(data=='company') {
            window.location.href = "filter.php";
        } else {
            alert("Wrong Username or Password!");

            // Call dialogue box
            var dlgtrigger = $('[data-dialog]'),
                somedialog = $(dlgtrigger).attr('data-dialog'),
                dlg = new DialogFx( somedialog );

            dlg.toggle.bind(dlg);

            //window.location.href = "indexwithlogin.php";
        }
    });
});

答案 1 :(得分:0)

使用 dlg.toggle.bind(dlg)()内部成功功能 代替 dlg.toggle.bind(dlg)