首页 > 解决方案 > 选择“否”单选按钮时打开的文本框,必须重新使用才能继续

问题描述

我迫切需要帮助。我有一个是和否单选按钮。否单选按钮需要打开一个隐藏的文本框,需要填写该文本框才能继续表单。我对 html 完全陌生,不知道如何完成这个。

标签: htmlinternet-explorerfirefoxadobecustom-wordpress-pages

解决方案


例如,它可以这样做:

function submitForm() {
  if (document.forms["form"]["no"].checked && document.forms["form"]["noTextarea"].value == "") { // If No option checked and if textarea´s value is empty ("")
    alert("If checked No, textarea must be filled.");
    return false;
  }
  else if (document.forms["form"]["yes"].checked == false && document.forms["form"]["no"].checked == false) { // If nothing is checked
    alert("Nothing was checked. You must select Yes or No.");
    return false;
  }
  else if (document.forms["form"]["yes"].checked) { // If checked Yes
    alert("Form OK.");
  }
  else { // Else (if checked No and textarea filled
    alert("Form OK.");
  }
}

function showTextarea() {
  if (document.forms["form"]["no"].checked) {
    document.forms["form"]["noTextarea"].style.display = "block";
  }
  else {
    document.forms["form"]["noTextarea"].style.display = "none";
  }
}
<form id="form" onsubmit="return submitForm()">
  <input type=radio name=yesnoRadio id=yes onclick=showTextarea()>
  <label for=yes>Yes</label>

  <input type=radio name=yesnoRadio id=no onclick=showTextarea()>
  <label for=no>No</label>

  <br>
  <textarea id="noTextarea" style="display: none;" placeholder="Requied when selecting 'No'"></textarea>

  <br>
  <input type=submit>
</form>


推荐阅读