无法从Javascript更改文本框的值

时间:2016-07-29 18:27:49

标签: javascript jquery html css

如果取消选中此复选框,我在此处尝试删除文本框中的任何文本。我试图使用document.getElementsById(textBoxID).value = ''来做到这一点,但它没有效果。我做错了什么。相应的代码如下所示。

<!DOCTYPE html>
<html>
<body>

<form action="demo_form.asp" method="get">
  <input type="text" name="YourEMail" id="YourEMail" style="width: 250px;"> 
  <input type="checkbox" onclick="enableDisable(this.checked, 'YourEMail')" name="check_sendToEmail" value="sendToEmail">
  <span style="color:cyan;">Send output to e-mail</span><br>
  <script>
  document.getElementById('YourEMail').disabled = true</script>
  </script>

  <script>
  function enableDisable(bEnable, textBoxID)
  {
   document.getElementById(textBoxID).disabled = !bEnable
   if (document.getElementById(textBoxID).disabled)
    document.getElementsById(textBoxID).value = ''
  }
  </script>
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

Typo @ getElementsById Value

是什么让你把s放在最后的陈述中?另请注意,属性区分大小写,因此valueValue不同。 Value

没有 InputElement 属性

&#13;
&#13;
document.getElementById('YourEMail').disabled = true

function enableDisable(bEnable, textBoxID) {
  document.getElementById(textBoxID).disabled = !bEnable
  if (document.getElementById(textBoxID).disabled)
    document.getElementById(textBoxID).value = ''
}
&#13;
<form action="demo_form.asp" method="get">
  <input type="text" name="YourEMail" id="YourEMail" style="width: 250px;">
  <input type="checkbox" onclick="enableDisable(this.checked, 'YourEMail')" name="check_sendToEmail" value="sendToEmail">
  <span style="color:cyan;">Send output to e-mail</span>
  <br>
</form>
&#13;
&#13;
&#13;