form object inside of ajaxForm error callback

穿精又带淫゛_ 提交于 2020-01-14 10:38:06

问题


I'm trying to access my form object inside of ajaxForm's error method:

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

error can take 3 params, but none of them are the form object, also this returns the url, but again no form.

Any suggestions?


回答1:


Tricky, why not use:

var myForm = $("#foo");

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

If there is another way, I'd love to know myself.




回答2:


In ajaxForm The form element itself is accessible in beforeSubmit section:

$('#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's my $('#foo') object?
    //It is here: myform
  }
});



回答3:


If you read the tab 'Working with Fields' in that plugin's docs, I think you'll find your answer.

For performance, you should probably store a reference to the form before you bind the ajaxForm.

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



回答4:


Does this not work? I.e.,

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


来源:https://stackoverflow.com/questions/1422143/form-object-inside-of-ajaxform-error-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!