Handling multiple forms with ajax
I'm trying to handle multiple forms with ajax. Suppose I have #formA, and #formB. And in the submitForm() function, I have the following:
function submitForm() {
var currentF开发者_StackOverflow中文版orm = $(this);
if (currentForm == "#formA") {
//do this
}
else if (currentForm == "#formB"){
//do that
}
}
However this approach is not working. What is the best way to deal with this? Thanks in advance.
Try modifying your code:
function submitForm() {
var currentForm = $(this);
if (currentForm.attr("id") == "formA") {
//do this
}
else if (currentForm.attr("id") == "formB") {
//do that
}
}
the code which you have written will not work, because you are trying to compare a jquery object with a string
.
you have to use attr
method of jquery to fetch the id of form and then compare it...something like this-
function submitForm() {
var currentForm = $(this);
if (currentForm.attr("id") == "formA") {
//do this
}
else if (currentForm.attr("id") == "formB") {
//do that
}
}
精彩评论