Reputation: 24305
I'm unable to trigger a form submission upon changing its input. I think I need event handling but I've never really learned what that is.
Here's my HTML:
<form action="/upload">
<input type="file" name="file">
</form>
Here's my JS:
$('input').change(function(e){
var closestForm=$(this).closest("form");
closestForm.submit(function(e) {
closestForm.ajaxSubmit({
success: function(data){
console.log('form submitted successfully');
}
}); //ajaxSubmit
return false;
}); //submit
}); //change
Please note I'd like to do the submit using AJAX but that isn't the issue as I have other parts of my codebase where I'm able to use the jQuery Form plugin ajaxSubmit()
no problem.
Thoughts?
Upvotes: 1
Views: 991
Reputation: 24305
I solved my own problem following the error message i got which led to the following SO Q&A. jQuery form plugin - Uncaught TypeError on file upload.
My error messages was similar to this:
jquery.form.js:357 Uncaught TypeError:
Object function (a,b){return new d.fn.init(a,b,g)} has no method 'handleError'
Basically as soon as I downloaded the latest jQuery Form plugin js file I was able to eliminate the submit()
function and just use ajaxSubmit()
as @Shusl suggested.
So special thanks to @Shusl for getting me on the right track.
Upvotes: 0
Reputation: 457
You could try to "lock" the form submission until the field was filled in, like this:
var form_locked = true;
$("input").change(function(e){
if($(this).val() == "") {
form_locked = true;
}else{
form_locked = false;
}
});
$("form").submit(function(e) {
// prevent default form submit
e.preventDefault();
if(form_locked) {
alert("Please fill in the field!");
}else{
// Do your form submission stuff here
}
});
Upvotes: 0
Reputation: 23208
Since you want submit form on input change. you don't require submit event on form
$('input').change(function(e){
var closestForm=$(this).closest("form");
closestForm.ajaxSubmit({
success: function(data){
console.log('form submitted successfully');
}
}); //ajaxSubmit
return false;
}); //change
Upvotes: 2