首页 > 解决方案 > 选择下拉菜单的选项时,“无法设置属性‘值’为空”

问题描述

我正在制作一个下拉菜单,一切似乎都运行良好,但是当我选择一个选项时,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";
           }
       };

我想这样当我选择列表的一个选项时,页面的一部分隐藏而另一部分出现。

我究竟做错了什么?

标签: javascripthtmltypeerror

解决方案


如果使用getElementById,则不需要使用#字符。选择器是必需的(querySelector例如方法)

此外,正如@I-wrestled-a-bear-once 所说,您需要在检查相等性时使用==or 。===


推荐阅读