Heim > Artikel > Web-Frontend > Verstehen der Grundlagen von JavaScript-Formularen_Javascript-Tipps
HTMLFormElement erbt HTMLElement und seine einzigartigen Eigenschaften und Methoden sind:
Die Referenz des Formularelements kann getElementById oder der numerische Index oder Namenswert in document.forms sein
1. Senden Sie das Formular ab
Es gibt drei Arten von Schaltflächen zum Absenden von Formularen:
<input type="submit" value="Submit Form"> <button type="submint">Submit Form</button> <input type="image" src="">
var form = document.getElementById("myForm"); form.addEventListener("submit", function () { event.preventDefault(); });
var form = document.getElementById("myForm"); form.submit();
var submitBtn = document.getElementById("submitBtn"); submitBtn.onclick = function () { //处理表格和提交等等 submitBtn.disabled = true; };
2. Formular zurücksetzen
Formular zurücksetzen sollte Eingabe oder Schaltfläche verwenden:
<input type="reset" value = "Reset Form"> <button type="reset">Reset Form</button>
var resetBtn = document.getElementById("resetBtn"); resetBtn.addEventListener("reset", function () { event.preventDefault(); });
var form = document.getElementById("myForm"); form.reset();
3. Formularfelder
Jedes Formular verfügt über ein Elementattribut, das eine Sammlung aller Formulare (Felder) im Formular ist:
var form = document.forms["myForm"]; var list = []; //取得表单中第一个字段 var firstName = form.elements[0]; list.push(firstName.name); //取得表单中名为lastName的字段 var lastName = form.elements["lastName"]; list.push(lastName.name); // 取得表单中包含的字段的数量 var fieldCount = form.elements.length; list.push(fieldCount); console.log(list.toString()); //firstName,lastName,4
<form id="myForm" name="myForm"> <ul> <li><input type="radio" name="color" value="red">red</li> <li><input type="radio" name="color" value="yellow">yellow</li> <li><input type="radio" name="color" value="blue">blue</li> </ul> <button type="submint">Submit Form</button> <button type="reset">Reset Form</button> </form>
var list = []; var form = document.forms["myForm"]; var radios = form.elements["color"]; console.log(radios.length) //3
Allgemeine Formularfeldattribute
verliert den Fokus und wird selten verwendet;
需要注意,第一个表单字段是input,如果其type特性为“hidden”,或者css属性的display和visibility属性隐藏了该字段,就会导致错误。
在HTML5中,表单中新增加了autofocus属性,自动把焦点移动到相应字段。
autofocus
如:
<input type="text" name="lastName" autofocus>
或者检测是否设置了该属性,没有的话再调用focus()方法:
window.addEventListener("load", function () { var form = document.forms["myForm"]; if (form["lastName"].autofocus !== true) { form["lastName"].focus(); }; });
共有的表单字段事件
除了支持鼠标键盘更改和HTML事件之外,所有的表单字段都支持下列3个事件:
blur: 当前字段失去焦点时触发;
change:input元素和textarea元素,在它们失去焦点且value值改变时触发;select元素在其选项改变时触发(不失去焦点也会触发);
focus:当前字段获得焦点时触发;
如:
var form = document.forms["myForm"]; var firstName = form.elements["firstName"]; firstName.addEventListener("focus", handler); firstName.addEventListener("blur", handler); firstName.addEventListener("change", handler); function handler() { switch (event.type) { case "focus": if (firstName.style.backgroundColor !== "red") { firstName.style.backgroundColor = "yellow"; }; break; case "blur": if (event.target.value.length < 1) { firstName.style.backgroundColor = "red"; } else { firstName.style.backgroundColor = ""; }; break; case "change": if (event.target.value.length < 1) { firstName.style.backgroundColor = "red"; } else { firstName.style.backgroundColor = ""; }; break; } }
以上就是本文的全部内容,希望对大家的学习有所帮助。