javascript - 选择下拉菜单的选项时,“无法设置属性‘值’为空”
问题描述
我正在制作一个下拉菜单,一切似乎都运行良好,但是当我选择一个选项时,y 给了我这个错误:
未捕获的类型错误:无法将属性“值”设置为 null
这是下拉列表的 HTML 代码:
<div id="dropdown">
<label for="tipo_envio">Seleccione el tipo de envío: </label>
<select name="tipo_envio" id="tipo_envio" onchange="hideElement()">
<option value="aereo">Aereo</option>
<option value="maritimo">Maritimo</option>
</select>
</div>
JS中的函数:
function hideElement() {
if (document.getElementById("#tipo_envio").value = "aereo") {
document.getElementById("#calculo_maritimo").style.display = "none";
document.getElementById("#calculo_aereo").style.display = "block";
} else {
document.getElementById("#calculo_aereo").style.display = "none";
document.getElementById("#calculo_maritimo").style.display = "block";
}
};
我想这样当我选择列表的一个选项时,页面的一部分隐藏而另一部分出现。
我究竟做错了什么?
解决方案
如果使用getElementById
,则不需要使用#
字符。选择器是必需的(querySelector
例如方法)
此外,正如@I-wrestled-a-bear-once 所说,您需要在检查相等性时使用==
or 。===
推荐阅读
- php - 使用有效密钥的 AES 256 ECB 解密返回奇怪/字符
- python - 如果数据库中已存在索引,如何向我的 slug 添加索引?
- ios - `NSMigrationManager`的默认源获取`fetchRequestForSourceEntityNamed:predicateString:`在哪里定义?
- scala - 偏斜数据的自定义分区器
- python - Python 类 __str__ 只返回一行字典
- angular - 更改 Moment 变量的小时数时,this.moment.hours() 不是函数错误
- php - 在评论中用名称替换 id 而不替换错误的大小写
- c++ - 处理自定义 Qt 小部件中的输入事件
- mybatis - SqlProvider 重载
- python - 如何在openpyxl中设置单元格语言以获得良好的数字格式?