Overriding check box in JavaScript with jQuery

前端 未结 3 1984
迷失自我
迷失自我 2020-12-07 04:32

Help with unit testing checkbox behavior. I have this page:




    
    

        
相关标签:
3条回答
  • 2020-12-07 05:32

    How about using change instead of click?

    $('#makeHidden').change(function() {
                var isChecked = $(this).is(':checked');
    
                if (isChecked) {
                    $('#displayer').hide();
                }
                else {
                    $('#displayer').show();
                }
                return false;
            });
    

    The return false; won't be in the way since the event is fired as a result of the change having occurred.

    0 讨论(0)
  • 2020-12-07 05:33

    Here is a work around.

    Now my code is like this:

            if ($.browser.msie) {
                $('#makeHidden').change(function () {
                    this.blur();
                    this.focus();
                    onCheckboxClicked();
                });
            }
            else {
                $('#makeHidden').change(function() {
                    return onCheckboxClicked();
                });
            }
    

    All my tests including manual toy and manual production are good.

    Anybody have something better than this hack?

    0 讨论(0)
  • 2020-12-07 05:38

    Try this:

    $(function() {
        $('<div><input type="checkbox" name="makeHidden" id="makeHidden" checked="checked" />Make Hidden</div>').appendTo('body');
        $('<div id="displayer" style="display:none;">Was Hidden</div>').appendTo('body');
    
        $('#makeHidden').click(function() { return onCheckboxClicked(this) } );
    });
    
    function onCheckboxClicked(el) {
        var isChecked = $(el).is(':checked');
    
        if (isChecked) {
            $('#displayer').hide();
        }
        else {
            $('#displayer').show();
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题