繁体   English   中英

使用Ajax处理多种表单

[英]Handling multiple forms with ajax

我正在尝试使用Ajax处理多种形式。 假设我有#formA和#formB。 在submitForm()函数中,我具有以下内容:

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
  } 
}

您编写的代码无法正常工作,因为您正在trying to compare a jquery object with a string

您必须使用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