Home >Web Front-end >JS Tutorial >Problem with checkbox initialization after refreshing in IE_Form special effects

Problem with checkbox initialization after refreshing in IE_Form special effects

WBOY
WBOYOriginal
2016-05-16 18:36:231126browse

At first glance, this function is very simple. It is a checkbox, add a script, and set the checked attribute of this checkbox to false.

Copy code The code is as follows:


<script>document.getElementById("chk_UnInital").checked=false;</script>

This code is used in firefox and other This requirement can be realized in browsers. Weidu cannot survive in IE. Every time the check is checked, whether it is refreshing or going back, the check is always checked.
Is this code not working under IE?
To verify, add an alert before and after this script, and find that the checkbox is unchecked before and after executing checked=false. When the warning box is confirmed, the checkbox appears instead!
This means that in the subsequent page loading sequence, an event sets this checkbox to true again. First try to execute this script in the page onload
Copy code The code is as follows:

<script> <br>window.onload=function(){ <br> alert("before"); <br> document.getElementById("chk_UnInital").checked=false; <br>} <br></script>

As expected, I found this when executing alert("before"); The check mark is selected, which means that the check mark 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, and this requirement is fulfilled.
However, onload will not be executed until all elements of the page have been loaded. During local testing, a request returned very slowly, causing the onload event to be delayed. This is something we do not want to see.
Later I discovered that another checkbox on the website would not save its previous state when the page was refreshed. After careful comparison, I found that this input had an extra checked="" attribute:

With this attribute, the event can work without placing it in onload
What is the reason for this?
Then do the following experiment:
Copy the code The code is as follows:









<script> <br>//window.onload=function(){ <br>var chks=document.getElementsByTagName("input"); <br>for(var i=0, l=chks.length;i<l;i ){ <br> alert(chks[i].id); <br> chks[i].checked=false; <br>} <br>//} <br></script>

For the convenience of description, here we call the execution time of the above script t1, the execution time of window.onload is called t3, and the execution time in between is called t2
After adding alert, we found that :
 Chk_UnInital chk_Inital
The script is executed in the script block:
Unselected before t1 Selected
Selected after t1 Unselected
The following is executed in onload:
Selected before t3 After selecting
t3, it is unselected. Unselected
It is found that after adding checked="", it is selected before t1, and then it is set to unchecked by t1.
Summary:
Option 1: Process in the onload event
Option 2: Add the checked attribute

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> //window.onload=function(){ var chks=document.getElementsByTagName("input"); for(var i=0,l=chks.length;i<l;i++){   //alert(chks[i].id);   chks[i].checked=false; } //} </script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn