如何用图像替换替换单选按钮?

Zac*_*bey 13 html css forms radio-button

我想,而不是标准的单选按钮,为每个单选按钮使用不同的图像.对于选定的状态,我希望在图像周围出现边框.

我已经尝试为单选按钮制作图像标签然后隐藏按钮,但这似乎因某种原因而破坏了功能.

我也遇到过这篇文章:http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/我可能会以某种方式扭曲我的目的.

有更简单/更好的方法吗?

Ari*_*Ari 30

就在这里!这是一个简单方法的例子:

不需要javascript

CSS

.radio_item{
display: none !important;
}

.label_item {
opacity: 0.1;
}

.radio_item:checked + label {
opacity: 1;
}
Run Code Online (Sandbox Code Playgroud)

HTML

<!--RADIO 1-->
<input type="radio" class="radio_item" value="" name="item" id="radio1">
<label class="label_item" for="radio1"> <img src="img_url_here"> </label>

<!--RADIO 2-->
<input type="radio" class="radio_item" value="" name="item" id="radio2">
<label class="label_item" for="radio2"> <img src="img_url_here"> </label>
Run Code Online (Sandbox Code Playgroud)

小提琴


Luc*_*ofi 22

jQuery的

        $('input:radio').hide().each(function() {
            $(this).attr('data-radio-fx', this.name);
            var label = $("label[for=" + '"' + this.id + '"' + "]").text();
            $('<a ' + (label != '' ? 'title=" ' + label + ' "' : '' ) + ' data-radio-fx="'+this.name+'" class="radio-fx" href="#">'+
                '<span class="radio' + (this.checked ? ' radio-checked' : '') + '"></span></a>').insertAfter(this);
        });
        $('a.radio-fx').on('click', function(e) {
            e.preventDefault();
            var unique = $(this).attr('data-radio-fx');
            $("a[data-radio-fx='"+unique+"'] span").attr('class','radio');
            $(":radio[data-radio-fx='"+unique+"']").attr('checked',false);
            $(this).find('span').attr('class','radio-checked');
            $(this).prev('input:radio').attr('checked',true);
        }).on('keydown', function(e) {
            if ((e.keyCode ? e.keyCode : e.which) == 32) {
                $(this).trigger('click');
            }
        });
Run Code Online (Sandbox Code Playgroud)
  • 演示源中的完整代码;


HMa*_*gdy 5

我可以提出两种不同的解决方案:

CSS解决方案:

/*
  Hide radio button (the round disc)
  we will use just the label to create push button effect
*/
input[type=radio] {
    display:none; 
    margin:10px;
}

/*
  Change the look'n'feel of labels (which are adjacent to radio buttons).
  Add some margin, padding to label
*/
input[type=radio] + label {
    display:inline-block;
    margin:-2px;
    padding: 4px 12px;
    background-color: #e7e7e7;
    border-color: #ddd;
}
/*
 Change background color for label next to checked radio button
 to make it look like highlighted button
*/
input[type=radio]:checked + label { 
   background-image: none;
    background-color:#d0d0d0;
}
Run Code Online (Sandbox Code Playgroud)

与HTML

<input type="radio" id="radio1" name="radios" value="all" checked>
   <label for="radio1">iPhone</label>
<input type="radio" id="radio2" name="radios"value="false">
   <label for="radio2">Galaxy S IV</label>
<input type="radio" id="radio3" name="radios" value="true">
   <label for="radio3">Nexus S</label> 
Run Code Online (Sandbox Code Playgroud)

JAVASCRIPT解决方案

在这里演示