如何在AjaxForm方法中获取表单的当前id?

时间:2013-10-15 22:58:35

标签: php jquery ajax forms uploader

$('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
}


}); 

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

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

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

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

3 个答案:

答案 0 :(得分:1)

在ajax表单插件中获取当前表单id的正确解决方案是:

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

答案 1 :(得分:1)

在@ 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);
        }
    });
}); 

答案 2 :(得分:0)

我发现了一种方法:

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

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


        }

感谢。

相关问题