1

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?

tim peterson
  • 23,653
  • 59
  • 177
  • 299

3 Answers3

2

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
Anoop
  • 23,044
  • 10
  • 62
  • 76
  • -@Shusl, thanks. Unfortunately that triggers an error specific to the jQuery form plugin. Do you have any recommendations while still including the submit event? – tim peterson Sep 27 '12 at 20:39
  • 1
    @timpeterson. if you are looking for file uploader then you can follow these thred http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery and http://stackoverflow.com/questions/2302344/jquery-form-plugin-file-upload. other wise you can use jQuery ajax. – Anoop Sep 27 '12 at 20:46
  • To everyone reading this accepted answer, please see my answer too for the complete instructions of how to tackle this problem. – tim peterson Sep 27 '12 at 20:51
0

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
    }

});
chris.ribal
  • 435
  • 2
  • 10
0

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.

Community
  • 1
  • 1
tim peterson
  • 23,653
  • 59
  • 177
  • 299