我已经编写了一个自定义对话框/模式框,如下所示:
<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>
答案 0 :(得分:0)
从第一个代码块中,您使用以下行调用对话框:
dlgtrigger.addEventListener( 'click', dlg.toggle.bind(dlg) );
...所以只需要在从AJAX调用中获得成功/完成响应时调用它。我对您的脚本进行了一些更改:
jqXHR
延迟对象.done()
代替.success()
。后者已被弃用而不是.done()
,而.error()
则相同,后者由.fail()
继承。这是修订后的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)