此代码将检测用户是否对表单上的其中一个输入进行了更改;
$(document).ready(function () {
var isDirty = false;
$(".formTable :input, select, textarea").change(function () {
isDirty = true;
});
但是在我的表单上我有一个弹出窗口,并且表单是使用 javascript 更新的。上面的代码没有选择这个,那么我该怎么做呢?
$('.dialogLink').live('click', function () {
var element = $(this);
// Retrieve values from the HTML5 data attributes of the link
var dialogTitle = element.attr('data-dialog-title');
var updateTargetId = '#' + element.attr('data-update-target-id');
var updateTargetId2 = '#' + element.attr('data-update-target-id2');
// Generate a unique id for the dialog div
var dialogId = 'uniqueName-' + Math.floor(Math.random() * 1000);
var dialogDiv = "<div id='" + dialogId + "'></div>";
// Load the form into the dialog div
$(dialogDiv).load(this.href, function () {
$(this).dialog({
bgiframe: true,
modal: true,
width: 500,
title: dialogTitle,
buttons: {
"Add": function () {
// Manually submit the form
var form = $('form', this);
$(form).submit();
},
"Cancel": function () { $(this).dialog('close'); }
}
});
// Enable client side validation
$.validator.unobtrusive.parse(this);
// Setup the ajax submit logic
wireUpForm(this, updateTargetId, updateTargetId2);
});
return false;
});
});
function wireUpForm(dialog, updateTargetId, updateTargetId2) {
$('form', dialog).submit(function () {
// Do not submit if the form
// does not pass client side validation
if (!$(this).valid())
return false;
// Client side validation passed, submit the form
// using the jQuery.ajax form
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
// Check whether the post was successful
if (result.success) {
// Close the dialog
$(dialog).dialog('close');
// UPDATE IS DONE HERE
$(updateTargetId).val(result.newTradeName);
$(updateTargetId2).val(result.newTradeId);
$(updateTargetId).attr("readonly", "readonly");
} else {
// Reload the dialog to show model errors
$(dialog).html(result);
// Enable client side validation
$.validator.unobtrusive.parse(dialog);
// Setup the ajax submit logic
wireUpForm(dialog, updateTargetId, updateTargetId2);
}
}
});
return false;
});
}