javascript - 复选框图像

标签 javascript html checkbox

我正在尝试制作一个只有图像和

我有来自 add an image to a html type input check box or radio 的代码:

<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=getImageUnchecked(i);
        } else {
            inputs[i].checked = 'checked';
            document.getElementById('checkImage'+i).src=getImageChecked(i);
        }
    }

    function getImageChecked(input) {
        if (input == 0)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.jpg";
        if (input == 1)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.jpg";
    }

    function getImageUnchecked(input) {
        if (input == 0)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.png";
        if (input == 1)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.png";
    }

    function startImages() {

    }

</script>
</html>
<head>
</head>
<body>
<input type="checkbox" id="option1" checked/> Test<br>
<input type="checkbox" id="option2" checked/> two<br>
<button onclick="alert('option 1 is checked? ' + document.getElementById('option1').checked
+ 'option 2 is checked? ' + document.getElementById('option2').checked)">Check</button>
<script type="text/javascript">replaceChecks();</script>
</body>

但是图像只会在第一次点击后才开始显示。 从页面加载开始,我可以做些什么吗? 我尝试了现有的功能,但一无所获。

最佳答案

您已将 checkClick() 附加到图像的点击事件,但您最初从未真正加载图像,因此您必须调用 checkClick(i)来自 for 循环。

<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';
                checkClick(i);
            }
        }
    }

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

    function getImageChecked(input) {
        if (input == 0)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.jpg";
        if (input == 1)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.jpg";
    }

    function getImageUnchecked(input) {
        if (input == 0)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.png";
        if (input == 1)
            return "https://azooree.com/wp-content/uploads/2017/08/Logomakr_6HaICv.png";
    }

    function startImages() {

    }
</script>
</html>
<head>
</head>
<body>
<input type="checkbox" id="option1" checked/> Test<br>
<input type="checkbox" id="option2" checked/> two<br>
<button onclick="alert('option 1 is checked? ' + document.getElementById('option1').checked
+ 'option 2 is checked? ' + document.getElementById('option2').checked)">Check</button>
<script type="text/javascript">replaceChecks();</script>
</body>

关于javascript - 复选框图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45512889/

相关文章:

html - Bootstrap 响应地垂直居中标题 div

javascript - 从 datepicker 获取值并分配给 php

html 复选框 url

javascript - 脚本在 JSFiddle 中有效,但在实际中无效

javascript - 当文本输入字段没有 ID 但有名称时,如何设置该字段的值。我无法使用 document.getElementById

javascript - 进行 Javascript 函数调用监听器/按键的最灵敏的方式是什么

javascript - 无法在 $http GET 调用中设置查询参数

html - 通过同时使用 min-width 和 max-width 来应用过渡

javascript - 如何拥有一个持久复选框,每次在 JQuery 中选中时都会打开或关闭 div?

php - 如何在提交前检查复选框数组的内容