【问题标题】:how to force users to write on a textarea when he/she selects a choice(like a checkbox)当他/她选择一个选项(如复选框)时,如何强制用户在文本区域上书写
【发布时间】:2026-01-11 12:35:02
【问题描述】:
<table>
  <tr>
    <td> 1. on Business units/Sections /customers :  </td>
    <td>
      <select>
      <option value="have a bussiness impact" id="bizyes">Yes</option>
      <option value="no bussiness impact" id="biznone"> No </option>
      </select>
    </td>
    <td> Describe (if Yes) </td>
    <td><input type="text" name="bizyesdis" size="90" maxlength="" id="bizyesdis"></td>
  </tr>
</table>

当用户从下拉列表中选择是时,他必须在此处指定的文本框中进行描述。

https://i.stack.imgur.com/uGKJ3.png

【问题讨论】:

  • 您可以使用javascriptjquery 来执行此操作。
  • 您的问题被否决了,因为该问题没有显示任何研究努力,也没有显示您尝试了什么。尝试使用 javascript 查看表单的客户端验证。

标签: javascript php jquery


【解决方案1】:

function isitrequired(){
  var e = document.getElementById("idoftheselect");
  var strUser = e.options[e.selectedIndex].value;
  if (strUser == "yes"){
  document.getElementById("idofthetextbox").required = true;
  }
  else{
  document.getElementById("idofthetextbox").required = false;
  }
 }
<!DOCTYPE html>
<html>
<body>


<form action="/action_page.php">
<select id="idoftheselect" onchange="isitrequired()">
  <option value="yes">yes</option>
  <option value="no">no</option>
</select>
  Username: <input type="text" id="idofthetextbox" name="usrname" required>
  <input type="submit">
</form>

</body>
</html>

使用 javascript 来做到这一点。

var e = document.getElementById("idoftheselect");
var f = e.options[e.selectedIndex].value;
if (f == "yes"){
document.getElementById("idofthetextbox").required = true;
}
else{
document.getElementById("idofthetextbox").required = false;
}

下拉菜单:

<select onchange="thatjavascriptfunctionabove()">
  <option value="yes">yes</option>
  <option value="no">no</option>
</select>

【讨论】:

  • 首先我要感谢您。但是:如您所见,我使用了您的解决方案,但不起作用:
  • 首先你在'validation()'之后缺少'{'。其次,您的浏览器中的开发控制台是否有任何错误?
  • 这个怎么样:
  • 对我仍然不起作用,因为您使用了 ,在 Yes 和 No 中都使用了 require 属性,它要求用户名。请记住,我的问题仅适用于“是”选项。
  • 上面的代码运行良好。有 sn-p 框,您可以在其中尝试一下。默认值为 yes,带有必需的标签。如果你把它改成没有人可以毫无问题地提交。
最近更新 更多