我有一个 jQuery 模式窗口,我在其中加载了一个带有提交按钮的表单。我想添加一个“取消”链接来清除表单并关闭弹出窗口。
我正在使用新的 ASP.NET MVC4 默认模板附带的代码。我正在寻找一个脚本来完成这项工作。
这是用于存储和显示对话框的当前代码:
$(function () {
// Cache for dialogs
var dialogs = {};
var loadAndShowDialog = function (id, link, url) {
var separator = url.indexOf('?') >= 0 ? '&' : '?';
// Save an empty jQuery in our cache for now.
dialogs[id] = $();
// Load the dialog with the content=1 QueryString in order to get a PartialView
$.get(url + separator + 'content=1')
.done(function (content) {
dialogs[id] = $('<div class="modal-popup">' + content + '</div>')
.hide() // Hide the dialog for now so we prevent flicker
.appendTo(document.body)
.filter('div') // Filter for the div tag only, script tags could surface
.dialog({ // Create the jQuery UI dialog
title: link.data('dialog-title') || 'Inloggen',
modal: true,
resizable: false,
draggable: false,
width: link.data('dialog-width') || 370,
beforeClose: function () { resetForm($(this).find('form')); }
})
.find('form') // Attach logic on forms
.submit(formSubmitHandler)
.find('a.close-dialog').click(function (id) {
//dialog close logic here
}).end();
});
};
var links = ['.loginLink'];
$.each(links, function (i, id) {
$(id).click(function (e) {
var link = $(this),
url = link.attr('href');
if (!dialogs[id]) {
loadAndShowDialog(id, link, url);
} else {
dialogs[id].dialog('open');
}
// Prevent the normal behavior since we use a dialog
e.preventDefault();
});
});