Let's keep them simple, shall we. First off, using pure HTML + CSS:
<div id="emotion">
<input type="radio" name="emotion" id="sad" />
<label for="sad"><img src="sad_image.png" alt="I'm sad" /></label>
<input type="radio" name="emotion" id="happy" />
<label for="happy"><img src="happy_image.png" alt="I'm happy" /></label>
</div>
This will degrade nicely if there's no JavaScript. Use id
and for
attributes to link up the label and radiobutton so that when the image is selected, the corresponding radiobutton will be filled. This is important because we'll need to hide the actual radiobutton using JavaScript. Now for some jQuery goodness. First off, creating the CSS we'll need:
.input_hidden {
position: absolute;
left: -9999px;
}
.selected {
background-color: #ccc;
}
#emotion label {
display: inline-block;
cursor: pointer;
}
#emotion label img {
padding: 3px;
}
Now for the JavaScript:
$('#emotion input:radio').addClass('input_hidden');
$('#emotion label').click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
The reason why we're not using display: none
here is for accessibility reasons. See: http://www.jsfiddle.net/yijiang/Zgh24/1 for a live demo, with something more fancy.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…