开发者

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

开发者 https://www.devze.com 2022-12-12 18:22 出处:网络
The following code works fine in FF, but doesn\'t in IE7 - when clicking the checkboxes in IE the divs doesn\'t toggle.

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');  

});


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');      
    });
}


You should use the click event to listen to the checking of a checkbox in IE.


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(); 
0

精彩评论

暂无评论...
验证码 换一张
取 消