我一直在研究一个通过 AJAX 加载的简单 JQuery 对话框表单。它使用 JQuery 工具进行验证,如果成功,则通过 AJAX 提交并关闭。这是通过 AJAX 打开对话框的代码(通过一个不错的小链接):
<script type="text/javascript">
var activeDialog;
$(function (){
$('a.ajax').click(function() {
var dialogDiv = '<div style="display:hidden" id="dialogDiv" title="'+this.title+'"></div>';
var dialog = $(dialogDiv).appendTo('body');
// load remote content
activeDialog = dialog.load(
this.href,
{},
function (responseText, textStatus, XMLHttpRequest) {
dialog.dialog({
resizable: true,
title: this.title,
autoOpen: true,
height: 350,
width: 600,
modal: true,
close: function(event, ui) {
try {$("#addNoteForm").data("validator").destroy();}catch(e){}
$(this).dialog("destroy");
}
});
}
);
return false;
});
});
</script>
<BR><BR>
<a class="ajax" href="dialog_clientEdit.php?cid=172" title="Add New Note">Create note</a>
如您所见,对话框是从“dialog_clientEdit.php”页面加载的。对话框使用自己的处理脚本加载,一旦填写并提交成功,通过 AJAX 发送数据,如果没有错误,则关闭自身并销毁验证器和对话框:
<div id="dialogNote9356904" title="Add New Note">
<form action="process_note.php" method="post" name="addNoteForm" id="addNoteForm" class="form has-validation">
<fieldset style="border:none;">
<div class="clearfix">
<label for="form-note" class="form-label">Note <em>*</em></label>
<div class="form-input form-textarea"><textarea id="form-note" rows="5" name="note" required="required" /></textarea></div>
</div>
<div class="clearfix">
<label for="form-notedate" class="form-label">Date <em>*</em><small>mm/dd/yyyy</small></label>
<div class="form-input"><input type="date" id="form-notedate" name="date" data-value="03/05/2004" format="mm/dd/yyyy" required="required" /></div>
</div>
<div class="clearfix">
<label class="form-label">Visibility <em>*</em><small>Private not visible to client</small></label>
<div class="form-input"><label for="form-visibility-private"><input type="radio" name="visibility" id="form-visibility-private" value="private" checked /> Private</label> <label for="form-visibility-public"><input type="radio" name="visibility" id="form-visibility-public" value="public" /> Public</label></div>
</div>
<div class="form-action clearfix">
<button class="button" id="submitNote" type="button" data-icon-primary="ui-icon-circle-check">Create Note</button>
<button class="button" type="button" onClick="activeDialog.dialog("close");">Cancel</button>
<span id="addDialogLoader"></span>
</div>
</fieldset>
</form>
</div>
<script>
$('#submitNote').click(function () {
var form = $('#addNoteForm');
if(form.data("validator").checkValidity()){
var formData = $(form).serialize();
// Save form via AJAX
var ajax_load = "<img src='../images/ajax-loader.gif' alt='Saving...' />";
var loadUrl = "process_note.php?cid=172";
$("#addDialogLoader").html(ajax_load).load(loadUrl, formData, function(response, status, xhr) {
if (status != "error") {
if(response == "1"){
activeDialog.dialog("close");
} else { alert("There was an error saving this note: "+response); }
} else {
alert("An error occurred while performing your request: " + xhr.status + " " + xhr.statusText);
}
});
}
return false;
});
</script>
问题是:表单只提交一次。您可以填写它,验证工作正常,并且所有 AJAX 都正确触发并且每个人都很高兴。然后,当您第二次尝试时,验证器不再工作,单击“创建注释”按钮基本上什么都不做……对话框永远不会提交,AJAX 永远不会触发。
我确保在对话框和验证器关闭后销毁它。对话框重新打开正常,但日期选择器在第二次打开时不起作用,没有更多的验证并且表单无法提交。
抱歉,我包含了所有代码,但我真的不知道错误在哪里。我确定这与我关闭它(或重新打开它,或两者兼而有之)的方式有关。任何人都可以帮忙吗?