我有一個ajax提交,無論我嘗試過什麼都不會運行。直到我找到一個包含$(function() {});
調用的提交的教程。然後我在一個函數調用中包裝了我的.submit,它似乎已經工作。令我難以置信的是,我有其他的.submit函數調用,而且不會被包裝在函數中。我想知道如果我在這裏錯過了一個我不明白的jQuery細微差別嗎?
爲了更充分地說明我的意思,這裏是我的功能之前和之後我在一個函數把它包起來......
之前...
$("#FormPutMsg1").submit(function (e) {
debugger;
e.preventDefault();
//animateSpinner();
var theURL = this.action;
var type = "POST";//this.methd;
var data = $(this).serialize();
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
dataType: "json",
success: function (result) {
debugger;
var data = result;
if (data.split(':')[0] == "Error") {
//$("#list").unblock();
$('#resultDiv').html('<b><p style="color: #ff00ff">' + data + '</p></b>');
setTimeout(function() {
$('#resultDiv').html("");
}, 10000);
}
else {
binddata(data);
}
}
});
return false;
});
後(這一個工程)。 ..
$(function() {
$("#FormPutMsg1").submit(function (e) {
debugger;
e.preventDefault();
//animateSpinner();
var theURL = this.action;
var type = "POST";//this.methd;
var data = $(this).serialize();
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
dataType: "json",
success: function (result) {
debugger;
var data = result;
if (data.split(':')[0] == "Error") {
//$("#list").unblock();
$('#resultDiv').html('<b><p style="color: #ff00ff">' + data + '</p></b>');
setTimeout(function() {
$('#resultDiv').html("");
}, 10000);
}
else {
binddata(data);
}
}
});
return false;
});
});
之前,我在常規提交功能prevaled功能包裹着我的功能(因爲e.preventDefault從來沒有叫我相信)。我錯過了一些先生們(和女士)?
我不是故意在這裏引起爭議,但我的代碼確實如此。它不包含在documenet.ready或任何類型的函數調用中。但是代碼仍然執行。我必須在那裏丟失一些東西。 – SoftwareSavant
'後'代碼包裝在「$(function(){})」中。我很困惑,爲什麼你說它不是。 「$(function(){})」將在頁面加載後執行代碼。 –
我的意思是說我在其他地方沒有包含其他代碼,或者確實有效。 – SoftwareSavant