我正在使用简单的模型,这是一段非常简洁的代码,但我有一个无法弄清楚的要求。
http://www.ericmmartin.com/simplemodal/
我的用例是第三个选项,我希望在用户单击操作后出现“确认弹出窗口”。问题在于,在示例中,消息是硬编码在 js 文件中的。
我需要能够传递此消息以及与“是”和“否”按钮关联的链接。
有没有人做过类似的事情。
我正在使用简单的模型,这是一段非常简洁的代码,但我有一个无法弄清楚的要求。
http://www.ericmmartin.com/simplemodal/
我的用例是第三个选项,我希望在用户单击操作后出现“确认弹出窗口”。问题在于,在示例中,消息是硬编码在 js 文件中的。
我需要能够传递此消息以及与“是”和“否”按钮关联的链接。
有没有人做过类似的事情。
查看页面的来源会告诉您您需要知道的一切。
<!-- Confirm -->
<link type='text/css' href='css/confirm.css' rel='stylesheet' media='screen' />
<script src='js/confirm.js' type='text/javascript'></script>
和
<div id='confirmDialog'><h2>Confirm Override</h2>
<p>A modal dialog override of the JavaScript confirm function. Demonstrates the use of <code>onShow</code> as well as how to display a modal dialog confirmation instead of the default JavaScript confirm dialog.</p>
<form action='download/' method='post'>
<input type='button' name='confirm' value='Demo' class='confirm demo'/><input type='button' name='download' value='Download' class='demo'/>
<input type='hidden' name='demo' value='confirm'/>
</form>
</div>
<div id='confirm' style='display:none'>
<a href='#' title='Close' class='modalCloseX simplemodal-close'>x</a>
<div class='header'><span>Confirm</span></div>
<p class='message'></p>
<div class='buttons'>
<div class='no simplemodal-close'>No</div><div class='yes'>Yes</div>
</div>
</div>
上面我们可以清楚地看到消息都在 HTML 中,而不是在 javascript 中。
然后,如果我们查看 confirm.js 的 JS 源代码,那么它就为您提供了如何初始化/触发它的全部内容。
/*
* SimpleModal Confirm Modal Dialog
* http://www.ericmmartin.com/projects/simplemodal/
* http://code.google.com/p/simplemodal/
*
* Copyright (c) 2009 Eric Martin - http://ericmmartin.com
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Revision: $Id: confirm.js 185 2009-02-09 21:51:12Z emartin24 $
*
*/
$(document).ready(function () {
$('#confirmDialog input.confirm, #confirmDialog a.confirm').click(function (e) {
e.preventDefault();
// example of calling the confirm function
// you must use a callback function to perform the "yes" action
confirm("Continue to the SimpleModal Project page?", function () {
window.location.href = 'http://www.ericmmartin.com/projects/simplemodal/';
});
});
});
function confirm(message, callback) {
$('#confirm').modal({
close:false,
position: ["20%",],
overlayId:'confirmModalOverlay',
containerId:'confirmModalContainer',
onShow: function (dialog) {
dialog.data.find('.message').append(message);
// if the user clicks "yes"
dialog.data.find('.yes').click(function () {
// call the callback
if ($.isFunction(callback)) {
callback.apply();
}
// close the dialog
$.modal.close();
});
}
});
}
我会推荐我使用的 BlockUI。使用此插件,它使用现有<div>
值来显示消息。在触发对话框触发的事件中,您可以使用 jQuery 通过正常的 DOM 操作来修改消息和链接文本,然后再<div>
按照应用程序的需要显示。
编辑 - 根据下面的第一条评论。
<script type="text/javascript">
$(document).ready(function() {
$('#test').click(function() {
$.blockUI({ message: $('#question'), css: { width: '275px' } });
});
$('#yes').click(function() {
// Remove the UI block.
$.unblockUI();
// Or you could use window.open
window.location = "http://www.google.com";
});
$('#no').click(function() {
$.unblockUI();
return false;
});
});
confirm.js 中给出的代码包含两个方法定义。一种是称为 的通用方法confirm
,它将创建带有您想要显示的消息的模式弹出窗口。每当您想创建模式弹出窗口时,您都必须使用此方法。
confirm("Are you sure you want to delete this item?", function() {
//Here you will write the code that will handle the click of the OK button.
});
在这里,第二个参数是一个函数(这几乎就像 C# 中的委托一样工作)。将会发生的是该confirm
函数将显示一个包含您的消息的对话框,并且当用户单击任何按钮时,将调用作为第二个参数传递的匿名函数。您还可以编写一个“普通”函数并将其作为第二个参数传递给confirm
-
function callbackHandler() {
//Here you will write the code that will handle the click of the OK button.
}
confirm("Are you sure you want to delete this item?", callbackHandler);
这段代码将调用您的函数 -
// if the user clicks "yes"
dialog.data.find('.yes').click(function () {
// call the callback
if ($.isFunction(callback)) { callback.apply(); }
// close the dialog
$.modal.close();
});