Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何显示图像以代替复选框?_Jquery_Css_Html - Fatal编程技术网

Jquery 如何显示图像以代替复选框?

Jquery 如何显示图像以代替复选框?,jquery,css,html,Jquery,Css,Html,我想在选中复选框和未选中空框时显示绿色选中图像。我试着把复选框放在div中并设置div的背景,但这对我没有帮助。知道怎么办吗?下面是我想要的输出 一种解决方案是使用div而不是checkbox,并根据需要设置背景,然后使用js将div的行为设置为checkbox,因此添加onClick操作。下面是一个示例,使用jQuery和CSS完成: $(“.checkbox”)。单击(函数(){ $(this.toggleClass('checked')) }); .checkbox{ 宽度:23px;

我想在选中复选框和未选中空框时显示绿色选中图像。我试着把复选框放在div中并设置div的背景,但这对我没有帮助。知道怎么办吗?下面是我想要的输出


一种解决方案是使用div而不是checkbox,并根据需要设置背景,然后使用js将div的行为设置为checkbox,因此添加onClick操作。

下面是一个示例,使用jQuery和CSS完成:

$(“.checkbox”)。单击(函数(){
$(this.toggleClass('checked'))
});
.checkbox{
宽度:23px;
高度:21px;
背景:透明url(http://i.stack.imgur.com/S4p2R.png )不重复0.50%
}
.检查{
背景:透明url(http://i.stack.imgur.com/S4p2R.png )不重复80%50%
}

正如其他人所说,您可以使用代理元素(div)并在单击div时更改复选框状态checked/unchecked。下面是您所需要的工作示例:(我使用了简单的javascript,以便读者能够快速理解它)

分步指南:

1) 首先,创建两个css类,一个用于选中状态,另一个用于未选中状态:

.image-checkbox {
    background:url(unchecked.png);
    width:30px;
    height:30px;
}

.image-checkbox-checked {
    background:url(checked.png);
    width:30px;
    height:30px;
}
2) 为div和复选框创建HTML。我们的想法是,对于每个复选框(input type=checkbox),我们将有一个div。div的id将用proxy这个词作为后缀,以便我们能够识别它。此外,对于每个代理div,我们将指定初始class.image复选框。假设我们创建两个复选框:

<div id="checkbox1_proxy" class="image-checkbox" />
<div id="checkbox2_proxy" class="image-checkbox" />

原始复选框(使用样式属性[可见性:隐藏]将其隐藏)


3) 现在,我们需要一些javascript代码,以便在单击代理元素(div)时将checkbox设置为checked/unchecked。此外,我们还需要根据复选框的当前状态更改代理div的背景图像。加载文档时,我们可以调用函数来附加事件处理程序:

<body onload="load()">

<script type="text/javascript">
function load() {
    var all_checkbox_divs = document.getElementsByClassName("image-checkbox");

    for (var i=0;i<all_checkbox_divs.length;i++) {

        all_checkbox_divs[i].onclick = function (e) {
            var div_id = this.id;
            var checkbox_id =div_id.split("_")[0];
            var checkbox_element = document.getElementById(checkbox_id);

            if (checkbox_element.checked == true) {
                checkbox_element.checked = false;
                this.setAttribute("class","image-checkbox");
            } else {
                checkbox_element.checked = true;
                this.setAttribute("class","image-checkbox-checked");
        }

        };
    }

}
</script>

函数加载(){
var all_checkbox_divs=document.getElementsByClassName(“图像复选框”);

对于(var i=0;i有人在谷歌搜索时绊倒了这一点?请考虑这种仅使用CSS的方法:

input[type=checkbox] {
    display: block;
    width: 30px;
    height: 30px;
    -webkit-appearance: none;
    outline: 0;
    background-repeat: no-repeat;
    background-position: center center;
    background-size: contain;
}

input[type=checkbox]:not(:checked) {
    background-image: url(unchecked.svg);
}

input[type=checkbox]:checked {
    background-image: url(checked.svg);
}


请参见此示例:
输入[类型=复选框]{
显示:块;
宽度:30px;
高度:30px;
背景重复:无重复;
背景位置:中心;
背景尺寸:包含;
-webkit外观:无;
大纲:0;
}
输入[类型=复选框]:选中{
背景图像:url('data:image/svg+xml;utf8');
}
输入[类型=复选框]:未(:选中){
背景图像:url('data:image/svg+xml;utf8');
}

知道这是一个较旧的线程,但需要一个将复选框转换为图像的解决方案。:)下面是一个对我来说非常有用的jQuery版本,希望与大家分享

function setCheckboxImageSrc(checkbox, image, checkedUrl, uncheckedUrl) {
    if (checkbox.is(":checked")) {
        image.attr("src", checkedUrl);
    } else {
        image.attr("src", uncheckedUrl);
    }
}

function setCheckboxImage(checkboxObj, className, checkedUrl, uncheckedUrl) {
    checkboxObj.hide();

    var $image = $("<img src='" + checkedUrl + "' />").insertAfter(checkboxObj);
    setCheckboxImageSrc(checkboxObj, $image, checkedUrl, uncheckedUrl);

    $image.click(function () {
        var $checkbox = $image.prev("." + className);
        $checkbox.click();
        setCheckboxImageSrc($checkbox, $image, checkedUrl, uncheckedUrl);
    });
}

$(".checkboxUp").each(function () {
    setCheckboxImage($(this), "checkboxUp", "../../../images/DirectionUpChecked.png", "../../../images/DirectionUpUnchecked.png");
});

$(".checkboxDown").each(function () {
    setCheckboxImage($(this), "checkboxDown", "../../../images/DirectionDownChecked.png", "../../../images/DirectionDownUnchecked.png");
});
函数setCheckboxImageSrc(复选框、图像、复选框、取消复选框){
如果(复选框为(“:选中”)){
attr(“src”,checkedrl);
}否则{
image.attr(“src”,不勾选durl);
}
}
函数setCheckboxImage(checkboxObj、类名、checkedUrl、uncheckedUrl){
checkboxObj.hide();
var$image=$(“”)。insertAfter(checkboxObj);
setCheckboxImageSrc(checkboxObj,$image,checkedrl,uncheckedUrl);
$image.单击(函数(){
var$checkbox=$image.prev(“.”+className);
$checkbox.click();
setCheckboxImageSrc($checkbox,$image,checkedrl,uncheckedUrl);
});
}
$(“.checkboxUp”)。每个(函数(){
setCheckboxImage($(this),“checkboxUp”、“../../../../images/DirectionUpChecked.png”、“../../../images/DirectionUpUnchecked.png”);
});
$(“.checkboxDown”)。每个(函数(){
setCheckboxImage($(this),“checkboxDown”、“../../../../images/DirectionDownChecked.png”、“../../../images/DirectionDownUnchecked.png”);
});

受@asim ishaq答案的启发,这里是一个基于jquery的解决方案,它还考虑到用户可以通过单击标签标签来切换复选框:

<style>
    div.round-checkbox {
        background: transparent url(/css/icons-sprite.png) no-repeat -192px -72px;
        height: 24px;
        width: 24px;
        display: inline-block;

    }

    div.round-checkbox.checked {
         background: transparent url(/css/icons-sprite.png) no-repeat -168px -72px;
     }

    input.round-checkbox {
        display: none;
    }
</style>

<div class="round-checkbox" data-id="subscribe-promo-1"></div>
<input type='checkbox' class="round-checkbox" name='subscribe' value='1' id="subscribe-promo-1" />
<label for="subscribe-promo-1">I want to subscribe to offers</label>


<script>
    $(document).ready(function () {
        var jRoundCheckbox = $('input.round-checkbox');

        /**
         * Init: if the input.checkbox is checked, show the checked version of the div.checkbox,
         * otherwise show the unchecked version
         */
        jRoundCheckbox.each(function () {
            var id = $(this).attr('id');
            if ($(this).prop('checked')) {
                $('.round-checkbox[data-id="' + id + '"]').addClass("checked");
            }
            else {
                $('.round-checkbox[data-id="' + id + '"]').removeClass("checked");
            }
        });

        /**
         * If the user clicks the label, it will check/uncheck the input.checkbox.
         * The div.checkbox should reflect this state
         */
        jRoundCheckbox.on('change', function () {
            var id = $(this).attr('id');
            $('.round-checkbox[data-id="' + id + '"]').toggleClass("checked");
            return false;
        });

        /**
         * When the user clicks the div.checkbox, it toggles the checked class;
         * also, the input.checkbox should be synced with it
         */
        $('div.round-checkbox').on('click', function () {
            var id = $(this).attr('data-id');
            var jInput = $('#' + id);
            jInput.prop("checked", !jInput.prop('checked'));
            $(this).toggleClass("checked");
            return false;
        });
    });
</script>

div.round-checkbox{
背景:透明url(/css/icons-sprite.png)无重复-192px-72px;
高度:24px;
宽度:24px;
显示:内联块;
}
div.round-checkbox.checked{
背景:透明url(/css/icons-sprite.png)无重复-168px-72px;
}
input.round-checkbox{
显示:无;
}
我想订阅优惠
$(文档).ready(函数(){
var jRoundCheckbox=$('input.round checkbox');
/**
*Init:如果选中了input.checkbox,则显示div.checkbox的选中版本,
*否则,显示未选中的版本
*/
jRoundCheckbox.each(函数(){
var id=$(this.attr('id');
if($(this.prop('checked')){
$('.round复选框[data id=“'+id+''“]).addClass(“选中”);
}
否则{
$('.round复选框[data id=“'+id+'”]).removeClass(“checked”);
}
});
/**
*如果用户单击标签,它将选中/取消选中input.checkbox。
*div.checkbox应反映此状态
*/
jrundcheckbox.on('change',function(){
var id=$(this.attr('id');
$('.round复选框[data id=“'+id+''“])。toggleClass(“选中”);
返回false;
});
/**
*当用户单击div.checkbox时,它切换选中的类;
*此外,input.checkbox应与其同步
*/
$('div.round-checkbox')。在('click',函数(){
var id=$(this.attr('data-id');
var jInput=$(“#”+id);
jInput.prop(“选中”),!jInput.prop(“选中”);
$(this.toggleClass(“选中”);
返回false;
});
});

这里是另一个示例。在Ionic v1上运行良好。 CSS

HTML


{{任何东西}
Th
<style>
    div.round-checkbox {
        background: transparent url(/css/icons-sprite.png) no-repeat -192px -72px;
        height: 24px;
        width: 24px;
        display: inline-block;

    }

    div.round-checkbox.checked {
         background: transparent url(/css/icons-sprite.png) no-repeat -168px -72px;
     }

    input.round-checkbox {
        display: none;
    }
</style>

<div class="round-checkbox" data-id="subscribe-promo-1"></div>
<input type='checkbox' class="round-checkbox" name='subscribe' value='1' id="subscribe-promo-1" />
<label for="subscribe-promo-1">I want to subscribe to offers</label>


<script>
    $(document).ready(function () {
        var jRoundCheckbox = $('input.round-checkbox');

        /**
         * Init: if the input.checkbox is checked, show the checked version of the div.checkbox,
         * otherwise show the unchecked version
         */
        jRoundCheckbox.each(function () {
            var id = $(this).attr('id');
            if ($(this).prop('checked')) {
                $('.round-checkbox[data-id="' + id + '"]').addClass("checked");
            }
            else {
                $('.round-checkbox[data-id="' + id + '"]').removeClass("checked");
            }
        });

        /**
         * If the user clicks the label, it will check/uncheck the input.checkbox.
         * The div.checkbox should reflect this state
         */
        jRoundCheckbox.on('change', function () {
            var id = $(this).attr('id');
            $('.round-checkbox[data-id="' + id + '"]').toggleClass("checked");
            return false;
        });

        /**
         * When the user clicks the div.checkbox, it toggles the checked class;
         * also, the input.checkbox should be synced with it
         */
        $('div.round-checkbox').on('click', function () {
            var id = $(this).attr('data-id');
            var jInput = $('#' + id);
            jInput.prop("checked", !jInput.prop('checked'));
            $(this).toggleClass("checked");
            return false;
        });
    });
</script>
input[type=checkbox] {
    display: none;
}

 :checked+img {
    content: url('/img/active.png');
}
    <label>
    <input type="checkbox" ng-model="anything">
    <img style="height:30px;" src="img/deactive.png">
    </label>
    <span>{{anything}}</span>