我有一个javascript使用ajax发布表单,但它一直给我一个错误。它是从一个自举按钮触发但似乎没有做任何事情?
按钮是:
<button id='btn-finish' name ='btn-finish' type='button' class='btn btn-primary'>Finish</button>
和js是: -
$(document).ready(function() {
$('#btn-finish').on('click', function() {
// Add text 'loading...' right after clicking on the submit button.
$('.output_message').text('Processing...');
var form = $(this);
$.ajax({
url: form.attr('process-form3.php'),
method: form.attr('method'),
data: form.serialize(),
success: function(result){
if (result == 'success'){
$('.output_message').text('Message Sent!');
} else {
$('.output_message').text('Error Sending email!');
// $('#5box').hide();
}
}
});
// Prevents default submission of the form after clicking on the submit button.
return false;
});
});
答案 0 :(得分:0)
您必须获取表单ID而不是按钮ID,您已经编写了获取按钮ID而不是表单对象的代码。
代码应该是例如:
<form id='test_form' action='path' method='post'>
<button id='btn-finish' name ='btn-finish' type='button' class='btn btn-primary'>Finish</button>
</form>
你的jquery代码:
$(document).ready(function(){ $('#btn-finish')。on('click',function(){
// Add text 'loading...' right after clicking on the submit button.
$('.output_message').text('Processing...');
var form = $('#test_form');
$.ajax({
url: form.attr('action'),
method: form.attr('method'),
data: form.serialize(),
success: function(result){
if (result == 'success'){
$('.output_message').text('Message Sent!');
} else {
$('.output_message').text('Error Sending email!');
// $('#5box').hide();
}
}
});
// Prevents default submission of the form after clicking on the submit button.
return false;
});
});