问题
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:
- You want to detect whether the user clicked the link to submit the form.
- You want to detect if the user clicked the submit button to submit the form.
- 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.
- Initialize global variable
wasClicked
tofalse
- When link is clicked set
wasClicked
totrue
- Trigger form submit after
wasClicked
is set. - Run
validate()
when form is submitted - Check
if(wasClicked){...}
invalidate()
Here is a Working Example
来源:https://stackoverflow.com/questions/30321252/how-to-differentiate-between-javascript-submit-and-manually-clicking-submit