简体   繁体   中英

jquery image selector not working in IE7

So I have some html like so:

<div id="avatar_choices">
    <label for="id_choice_0">
        <input id="id_choice_0" type="radio" name="choice" value="7" />
        <img src="someimage.jpg" />
    </label>
    <label for="id_choice_1">
        <input id="id_choice_1" type="radio" name="choice" value="8" />
        <img src="someimage2.jpg" />
    </label>
</div>

And some script:

$('#avatar_choices input').hide();
$('#avatar_choices img').click(function(){
    $('#avatar_choices img').css('border', '2px solid #efefef');
    $(this).css('border', '2px solid #39c');
    $(this).siblings('input').attr('checked', 'checked');
});

The goal is to allow the user to click around on the image options, having the selected one highlight with a border colour.

This works fine in FF. For some reason in IE once I click on an image, click another image, then try to click the first one, the border won't change (though it does get selected).

EDIT: My solution ended up happening half by accident. I changed the code to this due to redsquare's answer:

$('#avatar_choices input').hide();
$('#avatar_choices img').click(function(){
    $('#avatar_choices img').removeClass('selected');
    $(this).addClass('selected');
    $(this).siblings('input').attr('checked', 'checked');
});

where:

#avatar_choices img.selected{border:2px solid #39c;}

Go figure.

best using addClass and removeClass in this scenario. Easier to maintain. Can you paste your full html so I can see your doctype etc

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