您的位置:首页 > Web前端 > JavaScript

How to Submit a Form Using JavaScript

2010-03-17 17:24 525 查看

How to Submit a Form Using JavaScript

http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml

Generally, a form is submitted when the user presses a submit button. However, sometimes, you may need to submit the form programmatically using JavaScript.
JavaScript provides the form object that contains the submit() method. Use the 'id' of the form to get the form object.

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>


Click the link below to see the code in action:
JavaScript Form Submit Example 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: