我有以下脚本,这完美的作品,但问题是我需要一个form action attribute
它的工作,因此我的问题如何修改我的当前脚本,它可以防止默认表单提交行为和形式的提交当前页面上,而不需要一个action attribute
形式阿贾克斯防止默认和提交表格当前页
$(function() {
var form = $('#editRes');
var formMessages = $('#formMsg');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
//do the validation here
if (!validateLog()) {
return;
}
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
}).done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error').addClass('success');
// Set the message text.
$(formMessages).html(response); // < html();
// Clear the form.
$('').val('')
}).fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success').addClass('error');
// Set the message text.
var messageHtml = data.responseText !== '' ? data.responseText : 'Oops! An error occured!.';
$(formMessages).html(messageHtml); // < html()
});
});
function validateLog() {
var valid = true;
//VALIDATE HERE
return valid;
}
})
对不起,但我没有得到你。你能解释一下你想要的吗? – mrid
您好@mrid我的脚本只能用于
使用输入类型按钮而不是提交。在该按钮上注册单击事件并删除$(form).submit()函数。 $(button).click()将删除您的操作属性。 – ScanQR