Home > Web Front-end > JS Tutorial > body text

The difference between functions onclick and onsubmit

一个新手
Release: 2017-09-08 09:02:12
Original
2250 people have browsed it


The difference between functions onclick and onsubmit

onsubmit can only be used on forms and will be triggered before submitting the form. onclick is used by controls such as buttons to trigger click events.

Before submitting the form, data verification is generally performed. You can choose to verify in onclick on the submit button or in onsubmit.

But onclick is triggered earlier than onsubmit

1. The user clicks the button——>

2. The onclick event is triggered——>

3. onclick returns true or is not processed onclick --->

4. Triggers onsubmit event --->

5. onsubmit is not processed or returns true --->

6. Submit the form.

 onsubmit处理函数返回false,onclick函数返回false,都不会引起表单提交。
Copy after login

The first type: onsubmit

code block

<script language="javascript">
   function CheckPost ()
   {
      if (addForm.user.value == "")
      {
          alert("请填写用户名!");
          addForm.username.focus();          
          return false;
      }     
      if (addForm.title.value.length < 5)
     {
          alert("标题不能少于5个字符!");
          addForm.title.focus();          
          return false;
     }     return true;
   }
   </script>
   
   <form action="test.php" method="post" name="addForm"  onsubmit="return CheckPost();">
     <p>用户:<input type="text" size="10" name="user" maxlength="20"/></p>
     <p>标题:<input type="text" name="title" maxlength="50"/></p>
     <p>内容:<textarea name="content" rows="8" cols="30"></textarea></p>
     <p>
           <input type="submit" name="submit" value="发表留言"/>
     </p>
   </form>
Copy after login

The second type: onclick

<script language="javascript">
 2     function SendForm () 
 3     {
 4         if(CheckPost()) 
 5         { 
 6             document.addForm.submit(); 
 7         } 
 8     } 
 9 
 10     function CheckPost () 
 11     {
 12          if (addForm.user.value == "") 
 13          {
 14                alert("请填写用户名!");
 15                addForm.username.focus();
 16                return false;
 17          }
 18          if (addForm.title.value.length < 5) 
 19          {
 20                alert("标题不能少于5个字符!");
 21                addForm.title.focus();
 22                return false;
 23          }
 24          return true;
 25     }
 26 </script>
 27 
 28 <form action="test.php" method="post" name="addForm">
 29      <p>用户:<input type="text" size="10" name="user" maxlength="20"/></p>
 30      <p>标题:<input type="text" name="title" maxlength="50"/></p>
 31      <p>内容:<textarea name="content" rows="8" cols="30"></textarea></p>
 32      <p><input type="button" name="submit" value="发表留言" onclick="SendForm();"/></p>
 33 </form>
Copy after login


The above is the detailed content of The difference between functions onclick and onsubmit. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!