How do I style radio buttons with images - laughing smiley for good, sad smiley for bad?

后端 未结 10 1152
滥情空心
滥情空心 2020-11-30 19:56

I would like to create an HTML form for user feedback. If the overall feedback is good, the user should click on a laughing smiley, if the overall feedback is bad, the user

10条回答
  •  Happy的楠姐
    2020-11-30 20:29

    Let's keep them simple, shall we. First off, using pure HTML + CSS:

    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.

提交回复
热议问题