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

 

arrow
arrow
    創作者介紹
    創作者 Robert 的頭像
    Robert

    Robert 的部落格

    Robert 發表在 痞客邦 留言(0) 人氣()