How to deal with browser differences with indeterminate checkbox

前端 未结 4 830
夕颜
夕颜 2020-12-14 17:30

Primer: An HTML checkbox can be set as indeterminate, which displays it as neither checked nor unchecked. Even in this indeterminate state, the

相关标签:
4条回答
  • 2020-12-14 18:08

    Well make your own clickable image and use some java(script) to make it behave like that. I doubt dough how many users would understand this state, so be carefull where you use it.

    0 讨论(0)
  • 2020-12-14 18:13

    I am not sure that will using function to set value for indeterminate checkboxes will be good solutions because:

    • you will have to change every place where you are using them,
    • if user submit form without clicking on check-boxes, value that your backend will receive will be different depending of browser.

    But I like clever way to determinate how browser works. So you could check isCheckedAfterIndeterminate() instead usual window.navigator.userAgent.indexOf('Trident') >= 0 to see is it IE (or maybe other browser that works in unusual way).

    So my solution will be:

    /// Determine the checked state to give to a checkbox
    /// with indeterminate state, so that it becomes checked
    /// on click on IE, Chrome and Firefox 5+
    function isCheckedAfterIndeterminate()
    {
        // Create a unchecked checkbox with indeterminate state
        var test = document.createElement("input");
        test.type = "checkbox";
        test.checked = false;
        test.indeterminate = true;
    
        // Try to click the checkbox
        var body = document.body;
        body.appendChild(test); // Required to work on FF
        test.click();
        body.removeChild(test); // Required to work on FF
    
        // Check if the checkbox is now checked and cache the result
        if (test.checked) {
            isCheckedAfterIndeterminate = function () { return false; };
            return false;
        } else {
            isCheckedAfterIndeterminate = function () { return true; };
            return true;
        }
    }
    
    // Fix indeterminate checkbox behavoiur for some browsers.
    if ( isCheckedAfterIndeterminate() ) {
        $(function(){
            $(document).on('mousedown', 'input', function(){
                // Only fire the change event if the input is indeterminate.
                if ( this.indeterminate ) {
                    this.indeterminate = false;
                    $(this).trigger('change');
                }
            });
        });
    }
    
    0 讨论(0)
  • 2020-12-14 18:17

    If you would like to have an inderterminate checkbox which becomes checked on all browsers on click (or at least on IE, Chrome and FF5+), you need to initialise the checked attribute correctly, as shown here http://jsfiddle.net/K6nrT/6/. I have written the following functions to help you:

    /// Gives a checkbox the inderminate state and the right
    /// checked state so that it becomes checked on click
    /// on click on IE, Chrome and Firefox 5+
    function makeIndeterminate(checkbox)
    {
        checkbox.checked = getCheckedStateForIndeterminate();
        checkbox.indeterminate = true;
    }
    

    and the interesting function which relies on feature detection:

    /// Determine the checked state to give to a checkbox
    /// with indeterminate state, so that it becomes checked
    /// on click on IE, Chrome and Firefox 5+
    function getCheckedStateForIndeterminate()
    {
        // Create a unchecked checkbox with indeterminate state
        var test = document.createElement("input");
        test.type = "checkbox";
        test.checked = false;
        test.indeterminate = true;
    
        // Try to click the checkbox
        var body = document.body;
        body.appendChild(test); // Required to work on FF
        test.click();
        body.removeChild(test); // Required to work on FF
    
        // Check if the checkbox is now checked and cache the result
        if (test.checked)
        {
            getCheckedStateForIndeterminate = function () { return false; };
            return false;
        }
        else
        {
            getCheckedStateForIndeterminate = function () { return true; };
            return true;
        }
    }
    

    No image tricks, no jQuery, no extra attributes and no event handling involved. This relies only on simple JavaScript initialisation (note that the "indeterminate" attribute cannot be set in the HTML markup, so JavaScript initialisation would have been required anyway).

    0 讨论(0)
  • 2020-12-14 18:27

    This solved my problem

    $(".checkbox").click(function(){
        $(this).change();
    });
    
    0 讨论(0)
提交回复
热议问题