Home > Web Front-end > JS Tutorial > 使用js提交表单form

使用js提交表单form

WBOY
Release: 2016-06-01 09:54:55
Original
2482 people have browsed it

首先我们先用html创建一个表单

<code class="language-html"><form action="feedback.php" method="post">
 Name:<input type="text" name="username" size="30">
 <br><br>
 Email:<input type="text" name="useraddr" size="30">
 <br><br>
 <textarea name="comments" cols="30" rows="5">
 </textarea><br>
 <input type="button" value="Send Form">
</form></code>
Copy after login

在这个表单中,我们没用使用submit,我们准备使用js来提交这个表单。首先介绍一下js提交表单的方法。

首先需要获取到form表单,然后使用js的submit()方法来提交表单到服务器。语法如下:

<code class="language-javascript">document.getElementById("formid").submit();</code>
Copy after login

完整的代码如下:

<code class="language-html"><script>
function formSubmit()
{
     document.getElementById("form1").submit();
} 
</script>
<form action="feedback.php" method="post" id="form1">
 Name:<input type="text" name="username" size="30">
 <br><br>
 Email:<input type="text" name="useraddr" size="30">
 <br><br>
 <textarea name="comments" cols="30" rows="5">
 </textarea><br>
 <input type="button" onclick="formSubmit();" value="Send Form">
</form></code>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template