如何使用图像作为单选按钮

时间:2012-08-17 17:31:20

标签: javascript css html5

我正在尝试在表单上创建一个简单的启用/禁用单选按钮分组。我曾计划使用复选标记的图像启用和禁用x。我希望未单击的元素变为灰色以清除选中的元素。帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

尝试以下

<强> JQUERY

$(function() {
    $('input:radio').each(function() {
        $(this).hide();
        $('<a class="radio-fx" href="#"><div class="radio"></div></a>').insertAfter(this);
    });
    $('.radio-fx').live('click',function(e) {
        e.preventDefault();
          var $check = $(this).prev('input');
          $('.radio-fx div').attr('class','radio');
          $(this).find('div').attr('class','radio-checked');          
          $check.attr('checked', true);
    });
});

<强> CSS

.radio {
    background: url(radiobutton_no.png) no-repeat;
    width: 16px;
    height: 16px;
}
.radio-checked {
    background: url(radiobutton_yes.png) no-repeat;
    width: 16px;
    height: 16px;
}
相关问题