What can I use instead of the :indeterminate jQuer

2019-05-01 10:47发布

问题:

I have code like this

if ($('#chkCheckAll').is(':indeterminate') == true) 
{
}

But it is throwing exception in ie 8

what can do instead of this in Jquery to work with ie8

回答1:

Use this instead:

var $allChk = $('#chkCheckAll');
if ($allChk[0] && $allChk[0].indeterminate ) {
  ... 
}

The problem is that ':indeterminate' pseudo-expression is not supported by IE8 querySelectorAll implementation. But in your case, it's actually not required to use it, as you can query the corresponding property of DOM element itself



回答2:

Here is a great article http://css-tricks.com/indeterminate-checkboxes/

It SORT of works IE8 using .prop and has a hack too

<!-- Ghetto click handler -->
<input type="checkbox" id="cb1" onclick="ts(this)">function ts(cb) {
  if (cb.readOnly) cb.checked=cb.readOnly=false;
  else if (!cb.checked) cb.readOnly=cb.indeterminate=true;
}


回答3:

Enhancement of Ghetto handler to include the data in form submission:

<!DOCTYPE html>
<html>
    <head>
        <style>
            .nodisplay {display: none;}
        </style>
        <script>
            function ts(cb) {
                if (cb.readOnly) 
                    cb.nextSibling.checked 
                    = cb.checked
                    = cb.readOnly
                    = false;
                else if (!cb.checked) 
                    cb.nextSibling.checked 
                    = cb.readOnly
                    = cb.indeterminate=true;
            }
        </script>
    </head>
    <body>
        <form>

            <input type="checkbox" id="cb1" name="cb1" onclick="ts(this)"
            ><input type="checkbox" name="cb1i" />

            <input type="submit">

        </form>
    </body>
</html>

The result is either nothing (unchecked) cb1=on (checked) or cb1i=on (indeterminate). Note that for nextSibling to work in this case there must be no whitespace between the two checkboxes, otherwise it'll give you a text node.