检查Javascript表单,之后不显示元素

时间:2015-10-24 16:03:35

标签: javascript html5

我真的需要帮助Javascript中非常基本的东西。我有一个表格,我想要的是,如果选中单选按钮,将显示下一个问题。我认为我需要使用' If Else'但我不知道哪里出错了。

Javascript代码:

 function stageofproject() {
    if (document.getElementById("vooreenproject").checked = true) {
       document.getElementById("project").style.visibility = "visible";
    }
    else (document.getElementById("vooreenproject").checked = false) {
       document.getElementById("project").style.visibility = "hidden"; 
    }
    }
    stageofproject();

HTML:

<fieldset>
                <legend>Ik wil mij aanmelden:</legend>
                <li><label>
                    <input type="radio" name="submit-for" value="project" id="vooreenproject">
                    <span>Voor een project</span>
                </label></li>
                <li><label>
                    <input type="radio" name="submit-for" value="stage" id="alsstagebedrijf">
                    <span>Als stagebedrijf</span>

有人可以查看我的错误吗?

3 个答案:

答案 0 :(得分:0)

  

您需要===听众。同时使用===进行比较。 Boolean会将function stageofproject() { if (document.getElementById("vooreenproject").checked == true) { document.getElementById("project").style.visibility = "visible"; } else if (document.getElementById("vooreenproject").checked == false) { document.getElementById("project").style.visibility = "hidden"; } }值分配给元素。

<fieldset>
  <legend>Ik wil mij aanmelden:</legend>
  <li>
    <label>
      <input type="radio" checked name="submit-for" value="project" id="vooreenproject" onchange="stageofproject()">
      <span>Voor een project</span>
    </label>
  </li>
  <li>
    <label>
      <input type="radio" name="submit-for" value="stage" id="alsstagebedrijf" onchange="stageofproject()">
      <span>Als stagebedrijf</span>
    </label>
  </li>
</fieldset>
<div id="project">Question here</div>
{{1}}

答案 1 :(得分:-1)

看起来你需要两个等号来检查是否相等:

function stageofproject() {
  if (document.getElementById("vooreenproject").checked == true) {
     document.getElementById("project").style.visibility = "visible";
  }
  else if (document.getElementById("vooreenproject").checked == false) {
     document.getElementById("project").style.visibility = "hidden";       
    }       
  }
  stageofproject();

答案 2 :(得分:-1)

添加onclick属性,然后将stageofproject()功能放在

<input onclick="stageofproject();" type="radio" name="submit-for" value="project" id="vooreenproject">

<input onclick="stageofproject();" type="radio" name="submit-for" value="stage" id="alsstagebedrijf">