如何通过javascript提交表单form

来源:互联网 发布:面试linux运维故障处理 编辑:程序博客网 时间:2024/06/05 14:25

--How to Submit aForm Using JavaScript

Generally, aform is submitted when the user presses a submit button. However,sometimes, you may need to submit the form programmatically usingJavaScript.

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 JavaScriptcode for the submit call is:

document.forms["myform"].submit();


But, how to identify a form? Give an id attribute in the formtag

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

JavaScript Form Submit Example 1


<html><head>url</head>
<scriptsrc="http://code.jquery.com/jquery-latest.min.js"></script>

<body>
<formname="testForm" id="testForm"mothod="GET">
<inputtype="text" name="test1" id="order"value="order">
<inputtype="text" name="test2"value="get2">
<inputtype="submit" name="Submit" value="yes">
<ahref="javascript:submitform();">submit</a>

</form>
<scripttype="text/javascript">
functionsubmitform()
{
//document.forms['testForm'].submit();   //this willbe ok,here is name="testForm"
$('testForm').submit(); //hereis id="testForm"
}
function$(xxx)
{
returndocument.getElementByIdx_x_x_x(xxx);
}
</script>
</body>
</html>