Home >Web Front-end >HTML Tutorial >JavaScript learning (2) form element_html/css_WEB-ITnose
Reference of form object: document.forms [0] Or reference the name attribute, such as: document.forms["formname"], or you can directly use document.formname to call the form object
name, target , action, method, enctype
assignment change: document.forms[0].action or document.formName.action
To obtain the elements in the form, use form.elements[], as shown below to clear the text value Zero
var form=window.document.forms[0];for(var i=0;i<form.elements.length;i++){ if(form.elements[i].type=="text"){ form.elements.value=""; }}
<html><head><title></title><script type="text/javascript">function goThere () { var list=document.forms[0].urlList; location.href=list.options[list.selectedIndex].value;}</script></head><body><form name="radiolist"> <select name="urlList" onchange="goThere()"> <option selected value="http://www.baidu.com">baidu <option value="http://www.qq.com">qq </select></form></body></html>
javaScript provides a keyword this, which usually points to an object. This object contains scripts that use this keyword. Therefore, in a In the onchange event handler of the text field, you can use this as the keyword as the parameter of the function, such as
8a7510659db5ffdb1505e4a609d958cb
function upperMe(field){ //dosomething }
Each control has an attribute pointing to the contained table, so you can write this.form to get the form
<html><head><title>js_4</title><script type="text/javascript">function processData (formthis) { for(var i=0;i<formthis.Beatles.length;i++){ if(formthis.Beatles[i].checked){ break; } } var beatle=formthis.Beatles[i].value; var song=formthis.song.value; alert("chcecking whether "+song+" feature in " +""+beatle);}function varifySong(entry){ var song=entry.value; alert("checking whether "+song+" is a beatles tunes");}</script></head><body><form onsubmit="return false"><p>choose your favoriate Beatle:<input type="radio" name="Beatles" value="Jhon" checked>John<input type="radio" name="Beatles" value="Markey">Markey</p><p> input your song name:<br><input type="text" name="song" value="song search" onchange="varifySong(this)"><input type="button" name="process " value="process requset..." onclick="processData(this.form)"></p></p></form></body></html>
This section The code has a rather special logic. After experiments, we can find that after inputting in the input box and clicking process request, we can see that the first trigger is the onchange event of the text box, but the process request event is not executed. Because the text onChange event is triggered when the text leaves the focus, the onChange event will be triggered first when clicking anywhere other than the text, and the button click will not be executed until the second click. This is combined verification.
The onsubmit event handler must evaluate to return true to allow continued submission, or return false to prevent submission.
<html><head><title>js_5</title><script type="text/javascript"> function checkForm(fomr1){ for (var i = 0; i < fomr1.elements.length; i++) { if(fomr1.elements[i].type=="text" &&fomr1.elements[i].value==""){ alert("fill all the name"); return false; } }; }</script></head><body><form onsubmit="return checkForm(this)">Please enter all requested information:<br>FirstName: <input type="text" name="FirstName"><br>LastName :<input type="text" name="LastName"><br><input type="submit"></form></body></html>