将图像添加到 html 类型输入复选框或单选框

发布于 2024-10-19 03:03:46 字数 177 浏览 2 评论 0原文

我正在尝试指定一个图像,用于 html 输入类型复选框和单选框的未选中和选中值。

我得到了这个:

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

但它似乎不适用于单选按钮和复选框按钮。

有人知道有用的东西吗?

I'm trying to specify an image to be used for my unchecked and checked values for html input type checkbox and radio.

I have got this:

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

but it doesnt seem to work on radio and checkbox only button.

Does anyone know something that will work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦毁影碎の 2024-10-26 03:03:46

我已经找到并找到了答案。
在这些论坛的帮助下
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>

I have found and worked an answer.
with help from these forums
http://forums.digitalpoint.com/showthread.php?t=665980

all code is below: you can copy to a single file and add your own images and it will work.

<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>
江南烟雨〆相思醉 2024-10-26 03:03:46

看看这个 Ryan Fait - 自定义复选框和单选按钮。 ..希望这是您正在寻找的;)

Take a look at this Ryan Fait - Custom checkboxes and radio buttons ... Hope it's what you're looking for ;)

折戟 2024-10-26 03:03:46

在复选框中,如果您想更改图像或/和颜色,则优化方法是 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;
    }

In checkbox if you want to change images or/and color then the optimized way is http://jsfiddle.net/nsoni/4cHSB/5/ here you can click either on "item name" or on "checkbox" and get the effect.

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;
    }
吻泪 2024-10-26 03:03:46

您无法通过纯 CSS 设置复选框和单选按钮的样式。您需要一个 JavaScript 插件(有很多 jQuery 插件)来为您完成此操作。基本上,它通过保留默认元素的行为将另一个元素覆盖在默认元素上。

You cannot style checkboxes and radio buttons by pure CSS. You will need to get a JavaScript plugin (there are plenty of jQuery plugins out there) which will do this for you. Basically, it overlays another element over the default one by keeping the default element's behaviour.

小清晰的声音 2024-10-26 03:03:46

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

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;
}

Here is how I did it. I'm using radio input. The input boxes are hidden and the image in each label changes when the input is selected/not selected.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文