Home > Web Front-end > JS Tutorial > Detailed example of JavaScript form script

Detailed example of JavaScript form script

黄舟
Release: 2017-10-18 10:27:39
Original
1456 people have browsed it

The following editor will bring you a JavaScript-based form script (detailed explanation). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

What is a form?

A form has three basic components: Form tag: This contains the URL of the CGI program used to process the form data and the method for submitting the data to the server. Form fields: include text boxes, password boxes, hidden fields, multi-line text boxes, check boxes, radio button boxes, drop-down selection boxes and file upload boxes, etc. Form buttons: include submit buttons, reset buttons and general buttons; used to transfer data to CGI scripts on the server or cancel input. Form buttons can also be used to control other processing tasks with defined processing scripts.

The relationship between JavaScript and forms: The initial application of JS was to share the responsibility of the server for processing forms and break the dependence on the server. Although the web and JavaScript have made great progress, web forms still have not been used for this purpose. Many developers provide ready-made solutions for many common tasks, and many developers use JavaScript not only when validating forms, but also to enhance the default behavior of some standard form controls.

1. Basic knowledge of forms

In HTML, the form consists of the form tag. In JavaScript, the form corresponds to the HTMLFormElement type, HTMLFormElement type Inheriting the HTMLElement type, it has the same default attributes as other Element elements, and it also has its own attributes and methods:

acceptCharset: the character set that the server can handle; equivalent to accept-charset in HTML characteristic.

action: Accepts the requested URL; equivalent to the action attribute in HTML.

elements: A collection of all controls in the form (HTMLCollection).

enctype: Requested encoding type; equivalent to the enctype attribute in HTML.

length: The number of controls in the form.

method: The type of HTTP request to be sent, usually "get" or "post"; equivalent to the method attribute of HTML.

name: The name of the form; equivalent to the name attribute of HTML.

reset(): Resets all form fields to default values.

submit(): Submit the form.

target: The name of the window used to send requests and receive responses; equivalent to the target attribute of HTML.

The methods to obtain form form elements are: var form=document.getElementById('form1'); //Get form elements through id

var firstForm=document.forms[0] ; //Get all the form elements in the page through document.forms, and get the first form element through the index value '0'

var form2=document.forms['form2']; //Pass document.forms to get all the form elements in the page, and get the specific form element through the name value

Submit the form:


 <!-- 通用提交按钮 -->
 <input type="submit" value="Submit Form">
 <!-- 自定义提交按钮 -->
 <button type="submit">Submit Form</button>
 <!-- 图像按钮 -->
 <input type="image" src="graphic.gif">
Copy after login

Submit in this way form, the browser fires the submit event before sending the request to the server. This gives us the opportunity to validate the form data and use it to decide whether to allow the form submission. Blocking the default behavior of this event can cancel the form submission

In JS we can also programmatically call the submit() method to submit the form:


var form = document.getElementById("myForm");
//提交表单
 form.submit();
Copy after login

Prevent form submission (prevent default event):


var form = document.getElementById("myForm");
EventUtil.addHandler(form, "submit", function(event){
//取得事件对象
event = EventUtil.getEvent(event);
//阻止默认事件
EventUtil.preventDefault(event);
});
Copy after login

This technique can be used when the form data is invalid and cannot be sent to the server

Re- Reset form:


<!-- 通用重置按钮 -->
<input type="reset" value="Reset Form">
<!-- 自定义重置按钮 -->
<button type="reset">Reset Form</button>
Copy after login

When resetting the form, all form fields will be restored to their initial values ​​when the page is just loaded

Use JS method to reset Reset the form:


var form = document.getElementById("myForm");
//重置表单
form.reset();
Copy after login

Prevent the default action of resetting the form:


var form = document.getElementById("myForm");
EventUtil.addHandler(form, "reset", function(event){
//取得事件对象
event = EventUtil.getEvent(event);
//阻止表单重置
EventUtil.preventDefault(event);
});
Copy after login

Form fields:

Each form has an Element property, which is a collection of all form elements (fields) in the form. This collection is an ordered list. The order in which each form field appears in the element collection is the same as the order in which it appears in the tag. They can be accessed by position and name value. Common form fields include input, select, and fieldset. To obtain the form fields in the form:


var form = document.getElementById("form1");
//取得表单中的第一个字段
var field1 = form.elements[0];
//取得名为"textbox1"的字段
var field2 = form.elements["textbox1"];
//取得表单中包含的字段的数量
var fieldCount = form.elements.length;
Copy after login

Common form field attributes:

disabled: Boolean Value indicating whether the current field is disabled.

form: Pointer to the form to which the current field belongs; read-only.

name: The name of the current field.

readOnly: Boolean value, indicating whether the current field is read-only.

tabIndex: Indicates the switching (tab) sequence number of the current field.

type: The type of the current field, such as "checkbox", "radio", etc.

value: The value of the current field that will be submitted to the server. For file fields, this attribute is read-only and contains the path of the file on the computer.

Except the form attribute, any other attribute can be dynamically modified through JavaScript.

Being able to dynamically modify form field attributes means that we can dynamically operate the form at any time and in any way.

用户可能会重复单击表单的提交按钮。在涉及信用卡消费时,这就是个问题:因为会导致费用翻番。

为此,最常见的解决方案,就是在第一次单击后就禁用提交按钮。只要侦听 submit 事件,并在该事件发生时禁用提交按钮即可 :


//避免多次提交表单
 EventUtil.addHandler(form, "submit", function(event){
 event = EventUtil.getEvent(event);
 var target = EventUtil.getTarget(event);
//取得提交按钮
 var btn = target.elements["submit-btn"];
//禁用它
 btn.disabled = true;
 });
Copy after login

除了

之外,所有表单字段都有 type 属性。对于元素,这个值等于 HTML 特性 type 的值。对于其他元素,这个 type 属性的值如下表所列。

共有的表单字段方法 :

每个表单字段都有两个方法: focus()和 blur()。使用 focus()方法,可以将用户的注意力吸引到页面中的某个部位。例如,在页面加载完毕后,将焦点转移到表单中的第一个字段。


EventUtil.addHandler(window, "load", function(event){ /*给window绑定一个监听事件,放页面加载完成,光标自动对准在指定的表单字段*/
document.forms[0].elements[0].focus();
});
Copy after login

HTML5 为表单字段新增了一个 autofocus 属性。在支持这个属性的浏览器中,只要设置这个属性,不用 JavaScript 就能自动把焦点移动到相应字段。

例如:

与 focus()方法相对的是 blur()方法,它的作用是从元素中移走焦点:

document.forms[0].elements[0].blur();

change事件:对于