3 回答

TA貢獻1827條經(jīng)驗 獲得超8個贊
此代碼應該做到這一點。您不需要像以下這樣簡單的表單插件:
$('#create').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response) { // on success..
$('#created').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});

TA貢獻1820條經(jīng)驗 獲得超9個贊
這也適用于文件上傳
$(document).on("submit", "form", function(event)
{
event.preventDefault();
var url=$(this).attr("action");
$.ajax({
url: url,
type: 'POST',
dataType: "JSON",
data: new FormData(this),
processData: false,
contentType: false,
success: function (data, status)
{
$('#created').html(data); //content loads here
},
error: function (xhr, desc, err)
{
console.log("error");
}
});
});

TA貢獻1878條經(jīng)驗 獲得超4個贊
如果您不希望刷新頁面,則必須使用AJAX發(fā)布表單。
$('#create').submit(function () {
$.post('create.php', $('#create').serialize(), function (data, textStatus) {
$('#created').append(data);
});
return false;
});
添加回答
舉報