根据validator.min.js
尝试仅在有效时提交表单我试图让事情变得尽可能简单
[编辑] - 这已更新,因为我更改了发布时发现的一些简单错误。
$('#submit').on('click', function (e) {
$('#datafixForm').validator();
e.preventDefault();
var myForm = document.querySelector('#datafixForm');
var formData = new FormData(myForm);
var formIsValid = $('.form-group.has-error').length===0;
if (formIsValid) {
// Submit the form
$.ajax({
url: 'process.php',
method: 'POST',
data: formData,
success: function (data) {
console.log('Form submitted!');
console.log('Recieved' + data);
alert('Form submitted!');
},
error: function (data) {
console.log('We failed');
console.log('Got' + data);
alert('Didnt worky!');
}
});
alert("Request Raised!");
} else {
alert("Form is invalid");
}
});
和html:
<form method="post" role="form" id="datafixForm" name="datafixForm" enctype="multipart/form-data" data-toggle="validator">
<div class="row">
<div class="col-md-3">Ref : <div class="text-danger">*required</div></div>
<div class="col-md-3 form-group has-feedback">
<input class="form-control" name="Id" id="Id" placeholder="Txxx" required pattern="^C[0-9][0-9]" data-error="Please choose a valid ID" />
<div class="help-block with-errors"></div>
</div>
</div>
<div class="row">
<div class="col-md-7 form-group">
<button type="submit" id="submit" name="submit" class="btn btn-primary-outline center-block" >Submit</button>
</div>
</div>
内容的验证工作正常,当未填写必填字段时,表单是无效警报触发,但是当它有效时,我在控制台中显示错误:
Uncaught TypeError: Illegal invocation
似乎与$ .ajax有关({call