简体   繁体   English

使用Ajax处理多种表单

[英]Handling multiple forms with ajax

I'm trying to handle multiple forms with ajax. 我正在尝试使用Ajax处理多种形式。 Suppose I have #formA, and #formB. 假设我有#formA和#formB。 And in the submitForm() function, I have the following: 在submitForm()函数中,我具有以下内容:

function submitForm() {
  var currentForm = $(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 . 您编写的代码无法正常工作,因为您正在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- 您必须使用attr方法来获取表单的ID,然后对其进行比较...类似这样的东西-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM