Form object inside ajaxForm error callback

I am trying to access my form object inside the ajaxForm error method:

$('#foo').ajaxForm({
  error: function(){
    // where my $('#foo') object?
  }
});

the error can take 3 parameters, but none of them is a form object, it also returns a URL, but again no form.

Any suggestions?

+4
source share
4 answers

Difficult, why not use:

var myForm = $("#foo");

myForm.ajaxForm({
 error: function(){
  myForm.//whatever
 }
});

If there is another way, I would like to know myself.

+4
source

If you read the "Work with Fields" tab in these plagiarized documents, I think you will find the answer.

For performance, you should probably keep a link to the form before binding ajaxForm.

$(document).ready(function() {
    $foo = $('#foo');
    $foo.ajaxForm({
        error: function() {
            alert($('#fieldId', $foo).fieldValue()[0]);
        }
    });
});
0

In ajaxForm, the form element itself is available in the section beforeSubmit:

$('#foo').ajaxForm({

   beforeSubmit: function(formData, jqForm) {
        var myform = jqForm[0];
        /*
         If there are multiple forms in the selector, 
        each form is accessible with its order in the array
        */
   }

  error: function(){
    // where my $('#foo') object?
    //It is here: myform
  }
});
0
source

Does it work this? I.e.

$('#foo').ajaxForm({
  error: function(){
    alert($(this).attr('name'));
  }
});
-2
source

Source: https://habr.com/ru/post/1717661/


All Articles