简体   繁体   English

如何在复选框被选中的基础上显示div

[英]how to display the div on the basis of check box is checked

I am developing a game in that at the rules n regulation page i have a check box so i want when check box is checked continue button should appear otherwise it should be disappear from the page.我正在开发一个游戏,因为在规则和规则页面我有一个复选框,所以我希望在选中复选框时继续按钮应该出现,否则它应该从页面上消失。

<div id = "bSubmit"><input type="submit" value="continue"></div> 

function validate() {
    if (document.getElementById('iAgree').checked) 
    {
        bSubmit.display='block';
    } 
    else
    {
        bSubmit.display='none';
    }
}

Please help me out请帮帮我

Well there are points you need to change or add in your code.好吧,您需要在代码中更改或添加一些要点。 The one that I came up with is as follows.我想出的一个如下。

You can further improve it by separating the js into another file and wrapping your code in a document.onload function.您可以通过将 js 分离到另一个文件并将您的代码包装在document.onload函数中来进一步改进它。

<input type="checkbox" id="iAgree"/>
<div id = "bSubmit"><input type="submit" value="continue"></div> 

<script>

    var bIAgree = document.getElementById('iAgree');

    function validate() {

        var bSubmit = document.getElementById('bSubmit');

        if (bIAgree.checked) {
            bSubmit.style.display='block';
        } else {
            bSubmit.style.display='none';
        }
    }
    // For the first time that the page loads,
    // set the state of the div
    validate(); 

    // set the state of the div when the checkbox state has changed
    bIAgree.onchange = validate;

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM