我将尝试解释我的网站的结构,然后说明问题。
我在这一点上,我尝试了一些其他方法,但是没有起作用,例如嵌套表单:
button.btn.btn-warning(type='button' name='GCTTested' id='GCTTested' value='UpdateGctStatus' onclick="submitGlbForm('"+formList+"')") Save All
函数submitGlbForm,对于每个formID,我称之为submitForm函数:
function submitGlbForm(array){
array = array.split(',')
for(i=0; i<array.length; i++){
//console.log("SubmitGlbForm: "+array[i])
submitForm(array[i])
};
}
function submitForm(value){
//console.log("SubmitForm: "+value)
var frm = document.getElementById(value);
//console.log("Form: "+frm)
//console.log("Form: "+$('#'+value).serialize())
//console.log("Ajax submit action: "+$('#'+value).attr('action'))
frm.submit(function(e) {
e.preventDefault();
$.ajax({
type:'POST',
url:$('#'+value).attr('action'),
data: $('#'+value).serialize(),
success: function(data) {
alert('Save successful');
//alert(data);
},
error: function (data) {
alert('Error.');
//alert(data);
}
});
});
}
问题在于仅提交一种表单。恰好是最后一个。
有人可以帮助我找到为什么只提交一种表格或给我另一种方法吗?
谢谢
答案 0 :(得分:1)
submit — a button or attribute of button type that tells the browser to take action on the form (typically to send it to a server).
function submitForm(value){
//console.log("SubmitForm: "+value)
var frm = document.getElementById(value);
//console.log("Form: "+frm)
//console.log("Form: "+$('#'+value).serialize())
//console.log("Ajax submit action: "+$('#'+value).attr('action'))
$.ajax({
type:'POST',
async:false,
url:$('#'+value).attr('action'),
data: $('#'+value).serialize(),
success: function(data) {
alert('Save successful');
//alert(data);
},
error: function (data) {
alert('Error.');
//alert(data);
}
});
}
根据我的说法,如果一切都正确,那么您应该是这样的。无需在提交表单中编写Ajax。
您可以直接迭代并发送异步请求。循环的执行速度比ajax快,因此最后一个请求总是在循环内触发。您可以使用延期承诺来实现上述功能。访问link。