如何通过javascript提交表单form
  M9aMEIE19lAW 2023年11月02日 58 0


如何通过javascript提交表单form

 


--How to Submit a Form Using JavaScript

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>

JavaScript Form Submit Example 1


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

<body>
<form name="testForm" id="testForm" mothod="GET">
<input type="text" name="test1" id="order" value="order">
<input type="text" name="test2" value="get2">
<input type="submit" name="Submit" value="yes" >
<a href="javascript: submitform();">submit</a>

</form>
<script type="text/javascript">
function submitform()
{
// document.forms['testForm'].submit();   //this will be ok,here is name="testForm"
$('testForm').submit(); //here is id="testForm"
}
function $(xxx)
{
return document.getElementByIdx_x_x_x(xxx);
}
</script>
</body>
</html>

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  f18CFixvrKz8   2024年05月20日   88   0   0 JavaScript
  fxrR9b8fJ5Wh   2024年05月17日   52   0   0 JavaScript
  2xk0JyO908yA   2024年04月28日   40   0   0 JavaScript
M9aMEIE19lAW