Odd Checkbox Behaviour With Javascript / IE6
This is the chain of events:
1) There is a visible HTML checkbox, the user checks or unchecks the box
2) The HTML checkbox is hidden via javascript by setting the display to none
3) The HTML checkbox is made visible via javascript by setting the display to block
4) The user's selection reverts to default value of the checkbox, for example, if the checkbox was first rendered with 'checked=checked', and the user unchecks the box, it goes back to being checked. If it was not rendered with checked, and the user checks the box, it does back to being unchecked.
The checkbox is hidden when the user opens up another part of my web app. But when they go back, the checkbox shouldn't have changed... stupid IE6... anyways to fix this?
I even tried adding a onclick function which effe开发者_StackOverflow中文版ctively recreates a new checkbox object, destroys the odd and replaces it. This does not work either. Also, it doesn't work well since it doesn't stop this on behavior when there is no change on the checkbox.
Are you writing to the innerHTML
at any point in the script? For example, if you say element.innerHTML+='x'
on an ancestor element, it will destroy and recreate the entire contents of the ancestor, which will lose non-HTML-serialisable content, including form field state.
Otherwise, just setting display
on the checkbox should not affect its checkedness (and doesn't for me in IE6). However, setting the display
directly to block
is questionable as this is not the normal display value for a checkbox. To avoid having to worry about what the default checkbox display value is on various browsers, use CSS:
.hidden { display: none; }
and toggle the checkbox between hidden and shown states by adding and removing the className= 'hidden'
.
I am not sure, but maybe display none resets the checkbox on IE6.
I have never tried it and I have no IE6 to test on.
精彩评论