用ajax处理多个表单

问题描述:

我想用ajax处理多个表单。假设我有#formA和#formB。而在submitForm()函数,我有以下几点:用ajax处理多个表单

function submitForm() { 
    var currentForm = $(this); 
    if (currentForm == "#formA") { 
    //do this 
    } 
    else if (currentForm == "#formB"){ 
    //do that 
    } 
} 

但是这种方法是行不通的。处理这个问题的最佳方法是什么? 在此先感谢。

尝试修改代码:

function submitForm() { 
    var currentForm = $(this); 
    if (currentForm.attr("id") == "formA") { 
    //do this 
    } 
    else if (currentForm.attr("id") == "formB") { 
    //do that 
    } 
} 
+0

这很简单。谢谢。 –

,你已经写了都不行的代码,因为你是trying to compare a jquery object with a string

你必须使用jquery的attr方法来获取表单的id,然后比较一下...像这个 -

function submitForm() { 
    var currentForm = $(this); 
    if (currentForm.attr("id") == "formA") { 
    //do this 
    } 
    else if (currentForm.attr("id") == "formB") { 
    //do that 
    } 
}