我正在尝试在ajaxForm的错误方法中访问我的表单对象:

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

错误可能需要3个参数,但它们都不是表单对象,也会返回url,但是没有形式。

有什么建议吗?

有帮助吗?

解决方案

整蛊,为什么不使用:

var myForm = $("#foo");

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

如果有另一种方式,我很想知道自己。

其他提示

ajaxForm 表单元素本身可以在 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's my $('#foo') object?
    //It is here: myform
  }
});

如果您在该插件的文档中阅读了“使用字段”标签,我想您会找到答案。

为了提高性能,您可能应该在绑定ajaxForm之前存储对表单的引用。

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

不起作用?即,

$('#foo').ajaxForm({
  error: function(){
    alert($(this).attr('name'));
  }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top