【问题标题】:JS Check Form before submit提交前的JS检查表单
【发布时间】:2015-11-07 06:52:01
【问题描述】:

我有以下表格:

<form onsubmit="return testform(tolowanswers,emptyAnswers);" class="edit_question" id="edit_question" 

我在提交之前使用函数 testform 检查表单,是否有可能在 testform() 之后运行第二个函数

谢谢

【问题讨论】:

  • 新方法中你想做什么
  • 是的,您可以发布您的代码
  • return testform(tolowanswers,emptyAnswers) &amp;&amp; secondmethod();
  • @ArunPJohny 谢谢。您会将您的评论作为答案发布,以便我将其标记为解决方案吗?

标签: javascript jquery html


【解决方案1】:

一个简单的解决方案可以像这样调用函数

return testform(tolowanswers,emptyAnswers) && secondmethod();

如果testform 返回true,这将调用第二个函数。

另一种解决方案可以使用其他功能,例如

onsubmit="return myfunction();"

然后

function myfunction() {
    if (testfunction()) {
        secondfunction();
    } else {
        return false;
    }
}

【讨论】:

    【解决方案2】:

    您只需将这两个函数放入 onsubmit 事件本身,如下所示

    "return (testform(tolowanswers,emptyAnswers) && secondFn())"
    

    它会检查第一个函数是否为真,只有当它返回真时,才会执行第二个Fn。

    另一种方法是在所有验证后在 testForm 函数内调用 secondFn()

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 2012-07-17
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多