当前位置:网站首页>Basic use of form
Basic use of form
2022-07-02 05:26:00 【Sunm~】
form Attributes of the tag
1.action The value of the property should be a value provided by the backend url Address , This url The address is specifically responsible for receiving the data submitted by the form
When <form> Expressed in unspecified action In the case of attribute values ,action The default value of is... Of the current page url Address
Be careful : When the form is submitted , The page will immediately jump to action Property specified url Address
2. target Property is used to specify where to open action URL
3. method Property is used to specify how to submit form data to action url . Optional values are get and post
By default ,method The value of is get, Said by url Submit the form data to action url
Be careful : Get This method is suitable for submitting a small number of 、 Simple data ,
Post This method is suitable for submitting a large number of 、 complex 、 Or contain the data uploaded by the file
4. enctype Property is used to specify before sending form data How to encode data
Synchronous submission of forms and its disadvantages
1. What is synchronous submission of forms
By clicking on submit Button , Action that triggers form submission , So that the page jumps to action URL act , Synchronous submission of forms .
2. Disadvantages of form synchronous submission
- <form> After the form is submitted synchronously , The whole page will jump , Jump to action url The address pointed to , The user experience is terrible .
- < form> After the form is submitted synchronously , The status and data before the page will be lost .
3. How to solve the shortcomings of form synchronous submission
The form is only responsible for collecting data ,Ajax Responsible for submitting data to the server
Listen for form submission Events
1. stay jQuery in , Two ways can be used , Listen to the submission event of the form
<form action="/login" id="f1"> <input type="text" name="user_name" /> <input type="password" name="password" /> <button type="submit"> Submit </button> </form> <script> // The first way $(function() { // $('#f1').submit(function() { // alert(' Listening to the submission event of the form ') // }) // The second way $('#f1').on('submit', function() { alert(' Listening to the submission event of the form ') }) }) </script>
2. Block the default form submission behavior
When the supervisor hears the submission event of the form , You can call the... Of the event object event.preventDefault() function , To prevent form submission and page Jump , The sample code is as follows :
<script> // The first way $(function() { $('#f1').submit(function(e) { // Prevent form submission and page Jump e.preventDefault() }) // The second way // $('#f1').on('submit', function(e) { // Prevent form submission and page Jump //e.preventDefault() // }) }) </script>
3. Get form data quickly
Use serialize() function , The function example is as follows :
<script> // The first way $(function() { $('#f1').submit(function(e) { // Prevent form submission and page Jump e.preventDefault() var data = $(this).serialize() console.log(data); }) }) </script>
Be careful : In the use of serialize() Function to get form data quickly , You must add... For each form element name attribute
边栏推荐
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- h5跳小程序
- Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
- 7. Eleven state sets of TCP
- Fabric. JS upload local image to canvas background
- Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share
- Fabric.js IText设置指定文字的颜色和背景色
- ERP management system development and design existing source code
- 数据的储存
- Pyechart1.19 national air quality exhibition
猜你喜欢
随机推荐
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
来啦~ 使用 EasyExcel 导出时进行数据转换系列新篇章!
Straighten elements (with transition animation)
Using QA band and bit mask in Google Earth engine
JVM class loading mechanism
Principle and implementation of parallax effect
Fabric. JS iText superscript and subscript
Leetcode18题 【四数之和】递归解法
Visual studio import
Nodejs (03) -- custom module
视差特效的原理和实现方法
Global and Chinese market of commercial fish tanks 2022-2028: Research Report on technology, participants, trends, market size and share
js中的Map(含leetcode例题)
4. Flask cooperates with a tag to link internal routes
Go implements leetcode rotation array
Record sentry's path of stepping on the pit
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
"Original, excellent and vulgar" in operation and maintenance work
Fabric.js IText设置指定文字的颜色和背景色
Gee series: Unit 1 Introduction to Google Earth engine