2011-02-25 34 views
3

我想指定一个图像用于我的未经检查和检查值为html输入类型复选框和广播。添加图像到一个html类型的输入复选框或无线电

我有这样的:

background: url("image.png") no-repeat; 

,但它似乎没有在电台和复选框唯一的按钮工作。

有人知道会有用吗?

+0

希望这不是由于定位,复选框非常小相对于一个按钮?图像背景虽然适用,但可能因此而隐藏。 – adarshr 2011-02-25 12:19:46

回答

2

看看这个Ryan Fait - Custom checkboxes and radio buttons ...希望这是你在找什么;)

+0

我已经看到这个,但它看起来有点复杂,我想知道如果有一个更简单的方法吗? – f1wade 2011-02-27 10:00:10

+0

不知道任何,但不要害怕,它不像看起来那么复杂 – Elwhis 2011-02-27 10:43:05

+0

我只是试图让那个瑞安fait的东西工作,但图像从来没有工作。 – f1wade 2011-03-11 10:51:38

0

您不能使用纯CSS设置复选框和单选按钮的样式。你需要获得一个JavaScript插件(这里有很多jQuery插件),它们将为你做到这一点。基本上,它通过保持默认元素的行为覆盖默认元素上的另一个元素。

+0

所以没有办法在CSS中添加图像到收音机和复选框,最简单的方法来做到这一点与javascript – f1wade 2011-02-27 10:01:20

5

我已经找到并努力回答。 与帮助,从这些论坛 http://forums.digitalpoint.com/showthread.php?t=665980

所有代码如下:您可以复制到一个单一的文件,并添加自己的图像,它会工作。

<html> 
<script type="text/javascript"> 
//global variables that can be used by ALL the function son this page. 
var inputs; 
var imgFalse = '52 0 ROff.png'; 
var imgTrue = '52 0 ROn.png'; 

//replace the checkbox with an image and setup events to handle it 
function replaceChecks() { 
    //get all the input fields on the page 
    inputs = document.getElementsByTagName('input'); 

    //cycle trough the input fields 
    for(var i=0; i<inputs.length; i++) { 

    //check if the input is a checkbox 
    if(inputs[i].getAttribute('type') == 'checkbox') { 

     //create a new image 
     var img = document.createElement('img'); 

     //check if the checkbox is checked 
     if(inputs[i].checked) { 
      img.src = imgTrue; 
     } else { 
      img.src = imgFalse; 
     } 

     //set image ID and onclick action 
     img.id = 'checkImage'+i; 
     //set image 
     img.onclick = new Function('checkClick('+i+')'); 

     //place image in front of the checkbox 
     inputs[i].parentNode.insertBefore(img, inputs[i]); 

     //hide the checkbox 
     inputs[i].style.display='none'; 
    } 
} 
} 

//change the checkbox status and the replacement image 
function checkClick(i) { 
if(inputs[i].checked) { 
    inputs[i].checked = ''; 
    document.getElementById('checkImage'+i).src=imgFalse; 
} else { 
    inputs[i].checked = 'checked'; 
    document.getElementById('checkImage'+i).src=imgTrue; 
} 
} 

</script> 
</html> 
<body> 
<input type="checkbox" /> 
<script type="text/javascript">replaceChecks();</script> 
</body> 
+0

工作完美,没有任何问题。我刚刚开始感到困惑,因为你用空格写了图像的名字。谢谢! – 2013-04-06 19:50:31

0

这就是我做到的。我正在使用无线电输入。当输入被选中/未被选择时,输入框被隐藏并且每个标签中的图像改变。

的jsfiddle:http://jsfiddle.net/EhDsf/

HTML:

<div class="options"> 
    <label title="item1"> 
     <input type="radio" name="foo" value="0" /> 
     Item 1 
     <img /> 

    </label> 
    <label title="item2"> 
     <input type="radio" name="foo" value="1" /> 
     Item 2 
     <img /> 
    </label> 
    <label title="item3"> 
     <input type="radio" name="foo" value="2" /> 
     Item 3 
     <img /> 
    </label> 
</div> 

CSS:

div.options > label > input { 
    visibility: hidden; 
} 

div.options > label { 
    display: block; 
    margin: 0 0 0 -10px; 
    padding: 0 0 48px 0; 
    height: 20px; 
    width: 150px; 

} 

div.options > label > img { 
    display: inline-block; 
    padding: 0px; 
    height:60px; 
    width:60px; 
    background: url(https://cdn2.iconfinder.com/data/icons/snipicons/500/thumbs-down-128.png); 
    background-repeat: no-repeat; 
    background-position:center center; 
    background-size:60px 60px; 
} 

div.options > label > input:checked +img { 
    background: url(https://cdn1.iconfinder.com/data/icons/onebit/PNG/onebit_34.png); 
    background-repeat: no-repeat; 
    background-position:center center; 
    background-size:60px 60px; 
} 
1

在复选框,如果你想改变图像或/和颜色,然后优化的方式是http://jsfiddle.net/nsoni/4cHSB/5/这里您可以点击“项目名称”或“复选框”并获得效果。

HTML:

<div class="had"> 
     <div class="ch"> 
      <input type="checkbox" id="sho1" name="chk1"> 
      <div class="so"></div> 
      <label for="sho1">Select one</label> 
     </div> 
     <div class="ch"> 
      <input type="checkbox" id="sho2" name="chk2"> 
      <div class="so"></div> 
      <label for="sho2">Select two</label> 
     </div> 
    </div> 

CSS:

input { 
       display: none; 
    } 

     input [type=checkbox]:checked+.so{ 
       background-image:url('http://goo.gl/3tza1X'); 
       background-repeat:no-repeat;background-color:green; 
       border-radius:50%;background-position: -2px -1px; 
    } 

     input [type=checkbox]:checked+.so+label{ 
      color: red; 
    } 

     .so { 
      margin: 2px; float: left;height: 30px;width: 30px; 
      border: 3px solid #D8D8D8;background-color: #4679BD; 
      border-radius: 5px; 
    } 

     .show { 
       margin-left: 30px; 
    } 

     .had { 
       margin: 50px auto;width: 50%;border: 1px solid black; 
       height: 30px;padding:10px; 
    } 

     .ch { 
       float: left;margin-left:20px;width:40% 
    } 

     label { 
       height: 30px;width: 100%;position:relative; 
       display:block;margin-top: 5px; 
    } 
相关问题