close
For example, if the name of your form is ‘myform’, the JavaScript code for the submit call is:
document.forms["myform"].submit();
But, how to identify a form? Give an id attribute in the form tag
<form id='myform' action='formmail.pl'>
Here is the code to submit a form when a hyperlink is clicked:
<form name="myform" action="handle-data.php"> Search: <input type='text' name='query' /> <a href="javascript: submitform()">Search</a> </form> <script type="text/javascript"> function submitform() { document.myform.submit(); } </script>
表格驗證
<!-- Include the validator script--> <script src="/scripts/gen_validatorv2.js" type="text/javascript"></script> <form id="myform" action="handle-data.php"> Search: <input type='text' name='query' /> <A href="javascript: submitform()">Search</A> </form> <!-- Add validations to the form--> <script type="text/javascript"> var myformValidator = new Validator("myform"); myformValidator.addValidation("query","req", "Please enter the value for query"); </script> <!-- The function that submits the form--> <script type="text/javascript"> function submitform() { if(document.myform.onsubmit()) {//this check triggers the validations document.myform.submit(); } } </script>
Method 1 : Use the ‘Image’ input type
<form name="myform" action="handle-data.pl"> Search: <input type='text' name='query' /> <input type="image" src="go.gif" /> </form>
Method 2 : Use JavaScript Form Submit
<form name="myform" action="handle-data.php"> Search: <input type='text' name='query' /> <a href="javascript: submitform()"> <img src="go.gif" width="33" height="19" border="0" /> </a> </form> <script type="text/javascript"> function submitform() { if(document.myform.onsubmit && !document.myform.onsubmit()) { return; } document.myform.submit(); } </script>
來源:How to Submit a Form Using JavaScript
文章標籤
全站熱搜
留言列表