1
$('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 。怎么能这样做?

4

3 回答 3

1

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

beforeSubmit: function(formData, jqForm) {
     // return current form id
     var form = jqForm[0].id;
}
于 2014-08-01T16:53:19.640 回答
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);
        }
    });
}); 
于 2016-05-19T00:41:01.233 回答
0

我找到了一个方法 :

我添加了这个事件并工作了。

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


        }

谢谢。

于 2013-10-15T23:35:13.120 回答