html 为什么变量在if语句后返回不同的答案[关闭]

brjng4g3  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(82)

已关闭,此问题需要details or clarity。它目前不接受回答。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

5天前关闭。
Improve this question

function button() {
  var drop = document.getElementById("drop").value;
  console.log(drop);

  if (drop = "radio") {
    const para = document.createElement("button");
    para.innerHTML = "button";
    document.body.appendChild(para);
  } else if (drop = "button") {

  } else {

  }

};

个字符
我正在做一个谷歌表单的复制品,其中一个复选框将创建一个新元素。drop变量返回了不同的答案什么错了?

v440hwme

v440hwme1#

if语句中,我假设您希望将其与字符串进行比较,但代码为drop变量赋值了一个新值。您可以使用=====进行比较。

if (drop === "radio") { 
  // rest of the code
}

字符串
使用const(或let)而不是var也是一个好主意,这样您可能会得到一些IDE提示,并且更容易调试。

elcex8rz

elcex8rz2#

你的if语句应该是**==而不是=**

function button() {
    var drop = document.getElementById("drop").value;
    console.log(drop);

    if (drop == "radio") {
        const para = document.createElement("button");
        para.innerHTML = "button";
        document.body.appendChild(para);
    } else if (drop == "button") {

    } else {

    }

};

字符串

相关问题