首页 > 解决方案 > 复选框返回 null

问题描述

当复选框被选中时,它应该使一些元素为空并变为灰色。该功能几天前可以工作,但突然不行了,我真的不明白为什么。我在另一个函数中使用了它的元素,我不知道这是否相关,但我也会留下这些代码。问题是,它是如何返回“无法设置未定义的属性'backgroundColor'”的,首先是morada2,但它不返回null。它进入了 if,但它不能执行里面的内容。如果我更改document.getElementByID.checkedcheckBox.checked

var checkcheck = document.getElementById("checkcheck").value;
  var checkBox = document.getElementById("idtipomorada").value;
  var morada2 = document.getElementById("morada2").value;
  var cidade2 = document.getElementById("cidade2").value;
  var codpostal2 = document.getElementById("codpostal2").value;
  var concelho2 = document.getElementById("concelho2").value;
  var distrito2 = document.getElementById("distrito2").value;
  var pais2 = document.getElementById("pais2").value;

  function validate() {
    
    if (document.getElementById("idtipomorada").checked == true){
  
      console.log("erro aqui");
      morada2.style.backgroundColor = "rgb(150, 155, 153)";
      document.getElementById("morada2").disabled = true;
      cidade2.style.backgroundColor = "rgb(150, 155, 153)";
      document.getElementById("cidade2").disabled = true;
      codpostal2.style.backgroundColor = "rgb(150, 155, 153)";
      document.getElementById("codpostal2").disabled = true;
      concelho2.style.backgroundColor = "rgb(150, 155, 153)";
      document.getElementById("concelho2").disabled = true;
      distrito2.style.backgroundColor = "rgb(150, 155, 153)";
      document.getElementById("distrito2").disabled = true;
      pais2.style.backgroundColor = "rgb(150, 155, 153)";
      document.getElementById("pais2").disabled = true;
      checkcheck = "true";
  
    } else if (checkBox.checked == false) {
  
      morada2.style.backgroundColor = "white";
      document.getElementById("morada2").disabled = false;
      cidade2.style.backgroundColor = "white";
      document.getElementById("cidade2").disabled = false;
      codpostal2.style.backgroundColor = "white";
      document.getElementById("codpostal2").disabled = false;
      concelho2.style.backgroundColor = "white";
      document.getElementById("concelho2").disabled = false;
      distrito2.style.backgroundColor = "white";
      document.getElementById("distrito2").disabled = false;
      pais2.style.backgroundColor = "white";
      document.getElementById("pais2").disabled = false;
      checkcheck = "false";
    } else {
      console.log("erro");

    }
    return(checkcheck);
    console.log(checkcheck);
  }

function guardarMorada0() {

  
  var checked = document.getElementById("checkcheck").value;
  if (checked == "true") {
    guardarUma();
  } else {
    guardarDuas();
  }

}
<input type="checkbox" id="idtipomorada" onclick="validate()">
<label style="font-weight: lighter;">&nbspA minha morada de faturação é a mesma que a morada de envio</label>
<p id="checkcheck"></p>

,它不会识别 if 或 else,因此不会返回 backgroundColor 错误。我尝试将变量放在外部,内部,更改名称,将真假更改为字符串并返回,但没有任何效果。这也不是缓存问题。注意:该段落存在只是因为我需要验证返回值是否是我想要的。

标签: javascripthtml

解决方案


好的,解决了,基本上我不能做 getElementByID.value,只是 getElementByID 部分。我将其更改为 var morada2 = getElementById("morada2"); 它像以前一样完美地工作。


推荐阅读