【问题标题】:How can get current id of form in AjaxForm method?如何在 AjaxForm 方法中获取表单的当前 ID?
【发布时间】:2013-10-15 22:58:34
【问题描述】:
$('form').ajaxForm
({

beforeSend: function() 
{
// for example i need get current id form here how can ?
// codes
},
uploadProgress: function(event, position, total, percentComplete) 
{
// codes
},
success: function() 
{
// codes
},
complete: function(xhr) 
{
// codes
}


}); 

我在页面中使用了 2 个不同 id 的表单。例如:

<form id="sample1"></form>

<form id="sample2"></form>

我需要在 $('form').ajaxForm 中获取当前表单 ID。 怎么能这样?

【问题讨论】:

  • 您想要 ajaxForm 对象的哪个方法的 ID?我对该插件知之甚少,但该表单可以通过 $(this) 或您需要该表单的函数的参数之一内的某个位置获得。
  • [current id of form] 我在我的问题中说过。但是谢谢我可以解决我的问题。

标签: php jquery ajax forms uploader


【解决方案1】:

在 ajax 表单插件中获取当前表单 ID 的正确方法是:

beforeSubmit: function(formData, jqForm) {
     // return current form id
     var form = jqForm[0].id;
}

【讨论】:

  • 这可行,但为了更清楚起见,您应该从外部声明var form,并且只在 beforeSubmit 函数中分配它,这样其他方法就可以访问该值。
【解决方案2】:

在@a.4j4vv1 答案的帮助下,这里有一个完整的解决方案:

$(function(){

    var currentform; //declare variable here

    $("form[id^='sample']").ajaxForm ({ //"form[id^='sample']" means all forms with id starting with the word 'sample'

        beforeSubmit: function(formData, jqForm) {
            currentform = jqForm[0]; //retrieve form and assign variable here
        },

        beforeSend: function() {
            console.log("Ajax BeforeSend from "+currentform.id);
        },

        uploadProgress: function(event, position, total, percentComplete) {
            console.log("Ajax uploadProgress from "+currentform.id);
        },

        success: function() {
            console.log("Ajax success from "+currentform.id);
        },

        complete: function(xhr) {
            console.log("Ajax complete from "+currentform.id);
        }
    });
}); 

【讨论】:

    【解决方案3】:

    我找到了方法:

    我添加了这个活动并且工作了。

    beforeSerialize: function($form, options) 
            { 
            alert($form.attr('id'));
            return false;// return false to cancel submit  
    
    
            }
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 2015-10-15
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2019-03-20
      • 1970-01-01
      • 2015-08-15
      相关资源
      最近更新 更多