1
我要展示一些我的代碼。在我的剃鬚刀視圖中,我有一個HTMLForm,我想用它與Ajax。當我使用Ajax更新面板時,是否需要在那一刻設置所有事件?
看下面的代碼提交。
var editQuestionFormSubmit = function() {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
$('#target2').html(result);
//$("#editQuestionForm").submit(editQuestionFormSubmit);
}
});
return false;
};
var editQuestion = function() {
$.ajax({
url: this.href,
type: "get",
success: function (result) {
$("#target2").html = result;
$("#editQuestionForm").submit(editQuestionFormSubmit);
}
});
return false;
};
的editQuestion
功能是首次當用戶單擊超鏈接,它的結果中包含一種形式,這就是爲什麼我把它提交事件的原因。
現在,當用戶執行後,讓我們去想象,它不是有效的,所以我再次顯示相同的形式,但與現款車型
public ActionResult EditQuestion(QuestionTemplate qt)
{
if (ModelState.IsValid)
{
....
return ...
}
return PartialView("_EditQuestion", qt);
}
所以,最後的代碼我返回相同窗體,但顯示的錯誤進行驗證,我必須再次註冊提交事件處理程序。我正在做一個好的方法,還是有更好的方法來在每個回調中不註冊每個事件處理程序。