简体   繁体   中英

Clicking checkbox to expand div, doesn't work in IE7

The following code works fine in FF, but doesn't in IE7 - when clicking the checkboxes in IE the divs doesn't toggle.

How do I troubleshoot these kind of issues - does anyone have an idea on how to go about this?

// hide divs
$('.product-optional-toggle1').css({ display: 'none'}); 
$('.product-optional-toggle2').css({ display: 'none'}); 
$('.product-optional-toggle3').css({ display: 'none'}); 

// toggle divs when checkbox is checked
$('.product-optional-checkbox1').change(function () {
    if($(this).attr("checked") === "true") {
        $('.product-optional-toggle1').toggle('fast');  
        return;
    }
    $('.product-optional-toggle1').toggle('fast');  

});

$('.product-optional-checkbox2').change(function () {
    if($(this).attr("checked") === "true") {
        $('.product-optional-toggle2').toggle('fast');  
        return;
    }
    $('.product-optional-toggle2').toggle('fast');  

});

$('.product-optional-checkbox3').change(function () {
    if($(this).attr("checked") === "true") {
        $('.product-optional-toggle3').toggle('fast');  
        return;
    }
    $('.product-optional-toggle3').toggle('fast');  

});

您应该使用click事件来侦听IE中复选框的检查。

IIRC, IE onchange events for checkboxes behave oddly (compared to other browsers) and I solved it by using the click event insted.

(update: I'm too slow)

edit: you can simplify your code a bit too if you want...

for( var i = 1; i < 3; ++i ){
    $('.product-optional-toggle' + i).css({ display: 'none'}); 

    // toggle divs when checkbox is checked
    $('.product-optional-checkbox' + i).change(function () {
        if($(this).attr("checked") === "true") {
            $('.product-optional-toggle' + i).toggle('fast');  
            return;
        }
        $('.product-optional-toggle' + i).toggle('fast');      
    });
}

This should be browser neutral:

// toggle divs when checkbox is checked
$('.product-optional-checkbox1').click(function () {
    $('.product-optional-toggle1').toggle('fast');  
});

$('.product-optional-checkbox2').click(function () {
    $('.product-optional-toggle2').toggle('fast');  
});

$('.product-optional-checkbox3').click(function () {
    $('.product-optional-toggle3').toggle('fast');  
});

// hide divs
$('.product-optional-toggle1').hide(); 
$('.product-optional-toggle2').hide(); 
$('.product-optional-toggle3').hide(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM