how to differentiate between javascript submit and manually clicking submit

霸气de小男生 提交于 2019-12-23 09:42:20

问题


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

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

form:

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


Let me see if I understand:

  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.

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

  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()

Here is a Working Example



来源:https://stackoverflow.com/questions/30321252/how-to-differentiate-between-javascript-submit-and-manually-clicking-submit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!