简体   繁体   English

单选按钮显示div

[英]Radio button show div

So, i'm making a site for a project. 所以,我正在为一个项目做网站。 I have 2 radio buttons, if the radio button: 'Yes' is checked a div has to be shown on the same page at the moment when the radio button is checked. 我有2个单选按钮,如果已选中单选按钮:“是”,则在选中单选按钮时,必须在同一页面上显示div。 When the radio button 'no' is checked nothing has to happen and the div shouldn't be shown. 如果选中单选按钮“否”,则没有任何反应,并且不应显示div。 here's the thing is was trying but it just won't work. 这是尝试的事情,但这根本行不通。 I don't know what's wrong and can't find a solution on the web. 我不知道出了什么问题,也无法在网络上找到解决方案。

Here's my code: 这是我的代码:

<?php $ervaringja = 0; ?>

    Hebt u enige ervaring met Fitnessen? 
    <input type="radio" name="ErvaringJa_Nee"
    <?php if (isset($Ervaring) && $Ervaring=="Ja") $ervaringja = 1;?>
    value="Ja">Ja
    <input type="radio" name="ErvaringJa_Nee"
    <?php if (isset($Ervaring) && $Ervaring=="Nee") echo "checked";?>
    value="Nee">Nee<br>

    <?php 
    if ($ervaringja == 0){
        echo '<script>'. 'ervaringjanee()'. '</script>';
    }
    ?>

And the script: 和脚本:

<script>
    function ervaringjanee() 
        {
            document.getElementById("WelkeErvaring").style.display = "hidden";
        }
</script>

but the div keeps showing up, can some one help me? 但是div不断出现,有人可以帮我吗?

To hide an element, it's not display: hidden; 要隐藏一个元素,它不会display: hidden; , it's display: none; display: none; .

Also, what you write in PHP is done ONLY when the page is loading. 另外,仅在页面加载时才完成用PHP编写的内容。 To make a page change with PHP, you have to make it reload. 要使用PHP进行页面更改,必须重新加载页面。 What you do in JavaScript can happen at any time. 您在JavaScript中所做的事情随时都可能发生。 It usually reacts to events triggered by the user. 它通常对用户触发的事件做出反应。

I would advise putting only JavaScript in charge of showing/hiding the div. 我建议只让JavaScript负责显示/隐藏div。 Something like the code below: 类似于以下代码:

Hebt u enige ervaring met Fitnessen?
<input type="radio" name="ErvaringJa_Nee" value="Ja"
    <?php if (isset($Ervaring) && $Ervaring == "Ja") {echo 'checked="checked"';} ?>
/>
<input type="radio" name="ErvaringJa_Nee" value="Nee"
    <?php if (!isset($Ervaring) || $Ervaring == "Nee") { echo 'checked="checked"';} ?>
/>
<div id="WelkeErvaring">
    ....
</div>

<script>
var div = document.getElementById("WelkeErvaring");
var ja = document.querySelector("input[name=ErvaringJa_Nee][value=Ja]");
var nee = document.querySelector("input[name=ErvaringJa_Nee][value=Nee]");
function toggleDiv() {
    if (ja.checked) {
        div.style.display = "block";
    } else {
        div.style.display = "none";
    }
};
toggleDiv();
ja.addEventListener("change", toggleDiv);
nee.addEventListener("change", toggleDiv);
</script>

Manipulating the document with JavaScript also becomes easier if you use the jQuery plugin or similar, but this is not required 如果您使用jQuery插件或类似工具,使用JavaScript处理文档也将变得更加容易,但这不是必需的

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

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