我对
jquery很新,我正在寻找可以取代确认对话框的东西.我在
http://abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/#demo找到了jQuery Alert Dialogs
但是jConfirm不会返回与confirm()相同的值.
是否可以编写一个函数来获得相同的confirm()值?
关于回调函数的含义,我承认对我来说不是那么清楚:-)
但是jConfirm不会返回与confirm()相同的值.
是否可以编写一个函数来获得相同的confirm()值?
关于回调函数的含义,我承认对我来说不是那么清楚:-)
解决方法
jConfirm永远不会“返回”任何东西,因为它是“事件驱动的”.
jConfirm等待,直到用户做出决定,然后它将调用将处理答案的回调函数.但是jConfirm不会像标准确认(…)那样阻止代码执行流程.
因此,如果您之前的代码如下所示:
// ask for a decision
var answer = confirm("Leave website?"); // the whole script stops until the user has made a decision!
// process answer
if (answer){
alert("Bye bye!"); // the script waits here until the user has clicked "ok"
window.location = "http://www.google.com/";
}
else{
alert("Thanks for sticking around!"); // the script waits here until the user has clicked "ok"
}
它应该在jQuery中看起来像这样:
// prevIoUs code
// show confirm dialog but the script will not wait,the script execution goes forward
jConfirm('Leave website?','Confirmation Dialog',function(answer) {
// this is the callback function of the jConfirm dialog we made
// we arrive here when the user has made a decision
// the answer is true,he wants to leave
if (answer){
// show a jAlert Box
jAlert("Bye Bye!","Some Title",function() {
// this is the callback function of the jAlert Box
// we arrive here when the user has clicked "ok"
// send him to google
window.location = "http://www.google.com/";
});
}
else{
// show a jAlert Box without any callback
jAlert("Thanks for sticking around!","Some Title");
}
});
// the code that follows here will be immediately executed
// because unlike confirm(),jConfirm() will not block
// the code execution flow
并举例说明:
标准的JavaScript confirm()执行流程
| | | \/ confirm() waits for an answer... no further code will be executed until the user has made a decision | | \/ handle the user's answer | | \/ further code execution
jConfirm执行流程
| | \/ -------> jConfirm Dialog | | | | | \/ | Callback function | when the user has made | a decision. | (handle the answer here) | | \/ further code execution