【发布时间】:2011-02-25 15:07:20
【问题描述】:
我正在使用带有 AJAX/JQuery 的 Visual Studio 2010 和 MVC 3。我遇到的问题是代码发布了两次。仅当您发布某些内容然后再次发布时才会发生这种情况。
这是我的 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”并在新的发布后刷新cmets。
现在,问题来了。如果再次单击“创建评论”链接,而不重新加载页面并填写表单,“CreateComment”会被触发两次,“GetComments”也会被触发两次。 Firebug 也向我展示了这一点。
我在这里做错了什么?如果您在发布后重新加载页面 (F5),则不会执行此操作。但这违背了目的。应该能够多次提交评论而无需重新加载页面。
【问题讨论】:
标签: jquery asp.net-mvc asp.net-mvc-3 asp.net-mvc-controller