如何区分javascript提交和手动点击提交 [英] how to differentiate between javascript submit and manually clicking submit

查看:599
本文介绍了如何区分javascript提交和手动点击提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何区分JavaScript触发的提交和手动点击表单提交

以下示例代码

how to differentiate between javascript triggered submit and manually clicking form submit
sample code below

 function myfunction()
 {
    document.getElementById("id_searchform").submit();
    return true;
 }

形式:

<div class='row'>
   <div class='col-md-4'>
      <div class='clszipcode' ><span>Enter Zipcode</span></div>
   </div>
   <div class='col-md-4'>
      <div class='clstxtzipcode' ><input type="text" name="zip_code" id="txtZipcode"></div>
   </div>
   <div class='col-md-4'>
      <div class='clsbtnzip' ><input type="submit" name="submit" id="btnSearch" value="Search" class="button_example"  ></div>
   </div>
</div>
<a href="#"  onclick="return myfunction();" >click to submit</a>


推荐答案

让我看看我是否明白:


  1. 您想要检测用户是否点击了提交表单的链接。

  2. 您想要检测

  3. 您有另一个名为 validate()的函数,它将以某种方式使用这些信息。

  1. You want to detect whether the user clicked the link to submit the form.
  2. You want to detect if the user clicked the submit button to submit the form.
  3. You have another function called validate() which will use this information in some way.

如果是这种情况,请考虑使用变量来存储链接是否在触发表单提交之前被点击。 p>

If this is the case, consider using a variable to store whether the link was clicked before triggering the form to submit.


  1. 初始化全局变量 wasClicked false

  2. 点击链接时设置 wasClicked true

  3. 设置 wasClicked 后触发表单提交。

  4. 运行 validate $ c> validate()

  1. Initialize global variable wasClicked to false
  2. When link is clicked set wasClicked to true
  3. Trigger form submit after wasClicked is set.
  4. Run validate() when form is submitted
  5. Check if(wasClicked){...} in validate()

这里是工作示例

这篇关于如何区分javascript提交和手动点击提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆