Primer: An HTML checkbox can be set as indeterminate
, which displays it as neither checked nor unchecked. Even in this indeterminate state, there is still an underlying boolean checked
state.
When an indeterminate checkbox is clicked, it loses its indeterminate
state. Depending on the browser (Firefox), it can additionally toggle the checked
property.
This jsfiddle illustrates the situation. In Firefox, clicking either of the checkboxes once causes them to toggle their initial underlying checked
state. In IE, the checked
property is left alone for the first click.
I would like all browsers to behave the same, even if this means additional javascript. Unfortunately, the indeterminate
property is set to false before the onclick
handler (or onchange
and jquery change
) is called, so I can't detect whether it's called for a click on an indeterminate
checkbox or not.
The mouseup
and keyup
(for spacebar toggle) events show the prior indeterminate
state, but I'd rather not be that specific: it seems fragile.
I could maintain a separate property on the checkbox (data-indeterminate
or similar), but I wanted to know if there's a simple solution I'm missing, and/or if other people are having similar issues.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…