MVC 3 AJAX函数发布两次
我正在使用Visual Studio 2010和MVC 3与AJAX / JQuery。我遇到的问题是代码发布了两次。它只发生在你发布的东西上,然后再发一次。
这是我的JS:
$(document).ready(function () {
BindEvents();
});
function BindEvents() {
$('#OpenCommentDialog').click(function (event, ui) {
var id = $(this).attr('data');
$('#NewCommentDialog').dialog({
open: function (event, ui) {
$('#BugID').val(id);
var form = $('form', '#NewCommentDialog');
form.submit(function (e) {
var comment = form.serialize(true);
CreateComment(comment);
return false;
});
}
});
$('#NewCommentDialog').dialog('open');
return false;
});
}
function CreateComment(comment) {
if (comment != null) {
$.post('/Comments/Create/', comment, function (data) {
if (data == 'Success') {
var id = $.parseQuery(comment);
GetComments(id.BugID);
$('#NewCommentDialog').dialog('close');
}
});
}
}
function GetComments(id) {
$('#Comments').find('tr').remove();
$.getJSON('/Comments/GetComments', { id: id }, function (data) {
if (data != null) {
if (data.length > 0) {
$('#CommentListTemplate').tmpl(data).appendTo('#Comments');
}
}
});
}
我的HTML页面有一个调用CreateCommentPartial.cshtml文件的JQuery对话框。当用户填写它时,它会调用“CreateComment”函数,然后调用“GetComments”并在发布新评论后刷新评论。
现在,这是问题所在。如果再次单击“创建注释”链接,无需重新加载页面并填写表单,“CreateComment”将被触发两次,“GetComments”也会被触发两次。萤火虫也向我展示了这一点。
我在这做错了什么?如果您在发布后重新加载页面(F5),则不会执行此操作。但它失败了目的。一个人应该能够多次提交评论而无需重新加载页面。
没有找到相关结果
已邀请:
2 个回复
贸会
惜堡沁戚