我想通過jQuery Ajax提交上傳多個文件。如何防止瀏覽器在Ajax提交後重定向到FORM操作?
一切工作正常,除了提交後,瀏覽器轉到表單中指定的網址,即使我添加一個Return False
。
如果我使用$("#upload").serialize(),
而不是FormData
,它不會被重新定向!這裏是我的CoffeeScript代碼:
# Initialization code
$("#upload").submit ->
ajaxUpload()
return false
# Handler
ajaxUpload = ->
fd = new FormData()
files = $("#uploadFiles")[0].files
$.each files, (i, file) ->
fd.append("file" + i, file)
$.ajax
type: $("#upload").attr("method"),
url: $("#upload").attr("action"),
data: fd,
# data: $("#upload").serialize(),
success: (data, text, xhr) ->
debug "Success: " + JSON.stringify(data)
loadEventAgain()
error: (xhr, status) ->
debug "Error: " + JSON.stringify(xhr)
alert "done"
return false
你有沒有試過'e.preventDeafult();'? – 2012-07-23 11:43:37
我知道JS但不是CoffeeScript,所以也許這是一個愚蠢的問題,但是那個代碼的最後一行返回false?這是全部在事件處理程序或什麼? – nnnnnn 2012-07-23 11:44:52
是頁面上唯一的東西,因爲Ajax基本上會重新加載頁面的一部分。對於你的要求,我有點困惑。 – 2012-07-23 11:45:12