问题是你的
submitForms
函数不提交表单。它只是增加了一个
提交
试试这样的
// This is your AJAX submitter, it returns a deferred object
const ajaxSubmit = (form) => $.ajax({
url: form.action,
method: form.method,
data: $(form).serialize()
})
// Add individual submit handlers
const forms = $(".form").on("submit", e => {
e.preventDefault()
ajaxSubmit(e.target).done(data => {
// handle response data here if you want
})
})
// Submit all forms on click
$("#save_all").on("click", () => {
forms.each((_, form) => {
ajaxSubmit(form)
})
})
这是必须的
You might not need jQuery
版本
const ajaxSubmit = form => {
const data = new FormData(form)
const init = {
method: form.method
}
let url = form.action
if (form.method.toUpperCase() === "POST") {
init.body = form.enctype === "multipart/form-data" ? data
: new URLSearchParams(data)
} else {
url = url.replace(/\?.+$/, '') + `?${new URLSearchParams(data)}`
}
return fetch(url, init)
}
const forms = document.querySelectorAll("form.form")
forms.forEach(form => {
form.addEventListener("submit", e => {
e.preventDefault()
ajaxSubmit(e.target)
})
})
document.querySelector("#save_all").addEventListener("click", async () => {
await Promise.all(Array.from(forms, ajaxSubmit))
})