复选框onclick事件处理程序和显示/隐藏功能

时间:2022-12-10 00:01:59

so i am trying to make dynamic checkboxs that will show/hide certain content when clicked. I have this but cannot get it to work properly:

所以我试图制作动态复选框,在点击时显示/隐藏某些内容。我有这个,但无法让它正常工作:

<html> 
<head> 
    <title>Checkbox Event Handler</title> 
    <style type="text/css"> 
        #myGroup {visibility:hidden} 
    </style> 
    <script type="text/javascript"> 
        function toggle(chkbox, group) { 
            var visSetting = (chkbox.checked) ? "visible" : "hidden"; 
            document.getElementById(group).style.visibility = visSetting; 
        } 
        function swap(radBtn, group) { 
            var modemsVisSetting = (group == "modems") ? ((radBtn.checked) ? "" : "none") : "none"; 
            document.getElementById("modems").style.display = modemsVisSetting; 
        } 
    </script>
    <?php require_once("/var/www/html/exercise/Task/functions.php"); ?>
</head> 
<body> 
    <?php
        $seqA[]="AAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBB";
        $seqA[]="BBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCDDDDDDDDDD0";
        $seqA[]="CCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";
    ?>
    <form>
    <?php
        $i=o;
        foreach($seqA as $seq){
    ?>
            <input type="checkbox" name="monitor"onclick="toggle(this, 'myGroup')" />show results
            <span id="myGroup">
     <?php
            $score=rawtransform(950);
            $truecol= getcolor($score,220);
            colorSequence($seq,5/*hit*/,$truecol,4);
        }
      ?>
            </span>
    </form>
</body>
</html>

it opens the first string in $seqA normally and works fine however the second checkbox is within the first string ? Im sure ive done something very stupid, but im new to programming. Anyone help please ?

它正常打开$ seqA中的第一个字符串并且工作正常但第二个复选框在第一个字符串中?我确定我做过一些非常愚蠢的事情,但我对编程很陌生。有人帮忙吗?

2 个解决方案

#1


1  

The problem I see is that in each foreach loop iteration you add element span with the same id attribute. Set unique IDs, change your JavaScript toggle function to address specified span and it will work.

我看到的问题是,在每个foreach循环迭代中,您添加具有相同id属性的元素跨度。设置唯一ID,更改JavaScript切换功能以解决指定的范围,它将起作用。

#2


0  

I'm not very familiar with PHP, but it appears that you are generated multiple span elements with the same ID, which would cause the document.getElementById to act in unpredictable ways.

我对PHP不是很熟悉,但看起来你生成了具有相同ID的多个span元素,这会导致document.getElementById以不可预测的方式运行。

#1


1  

The problem I see is that in each foreach loop iteration you add element span with the same id attribute. Set unique IDs, change your JavaScript toggle function to address specified span and it will work.

我看到的问题是,在每个foreach循环迭代中,您添加具有相同id属性的元素跨度。设置唯一ID,更改JavaScript切换功能以解决指定的范围,它将起作用。

#2


0  

I'm not very familiar with PHP, but it appears that you are generated multiple span elements with the same ID, which would cause the document.getElementById to act in unpredictable ways.

我对PHP不是很熟悉,但看起来你生成了具有相同ID的多个span元素,这会导致document.getElementById以不可预测的方式运行。