What can I use instead of the :indeterminate jQuery selector to avoid an exception in IE 8?

本秂侑毒 提交于 2019-12-22 06:43:08

问题


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.



来源:https://stackoverflow.com/questions/14625719/what-can-i-use-instead-of-the-indeterminate-jquery-selector-to-avoid-an-excepti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!