There is a requirement in the project that requires the checkbox on the page to be re-initialized when the page is refreshed or rolled back, that is, it is in the unselected status and does not save the previous status. In the first day, this function is very simple. In a checkbox, add a script to set the checked attribute of this checkbox to false.
The Code is as follows:
Script document. getElementById ("chk_uninfound"). checked = false; script
This code can meet this requirement in firefox and other browsers. vidu cannot live or die in IE. This check is checked every time, whether it is Refresh or backward.
Is this code in IE not working?
Verify that an alert is added before and after the script. It is found that the checkbox is not selected before and after the checked = false is executed. When the warning box is confirmed, this check box appears!
This indicates that in the subsequent page loading sequence, an event sets this checkbox to true. First, try to execute this script in the onload of the page.
The Code is as follows:
Script
Window. onload = function (){
Alert ("before ");
Document. getElementById ("chk_uninfound"). checked = false;
}
Script
We found that this check box was selected when executing alert ("before");, which means that this check box was automatically selected by the browser before the onload event, the specific reason involves the internal mechanism of IE.
Register this function in the onload event. This requirement is met here.
However, onload is executed only after all the elements on the page are loaded. During the local test, a request returns slowly, so that the onload event cannot be executed, this is something we don't want to see.
Later I found that another checkbox on the Website won't save the previous status when the page is refreshed. After careful comparison, I found that this input has one more checked = "" attribute:
With this attribute, the event does not need to be placed in onload.
Why?
Next, perform the following experiments:
The Code is as follows:
This checkbox can't be inapplicable
This checkbox can be inbuilt
Script
// Window. onload = function (){
Var chks = document. getElementsByTagName ("input ");
For (var I = 0, l = chks. length; I Alert (chks [I]. id );
Chks [I]. checked = false;
}
//}
Script
For ease of description, the execution time of the above script is called t1, And the execution time of window. onload is called t3.
After adding alert, we found that:
Chk_unin1_chk_in1 _
Run the script in the script block:
Selected before t1
Selected after t1
The following code is executed in onload:
Selected before t3
Unselected after t3
After checked = "" is added, it is selected before t1 and then set as unchecked by t1.
Summary:
Solution 1: Process in the onload event
Solution 2: add the checked attribute
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME = "Generator" CONTENT = "EditPlus"> <meta name = "Author" CONTENT = ""> <meta name = "Keywords" CONTENT = ""> <meta name = "Description "CONTENT =" "> </HEAD> <BODY> </HTML>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]