简体   繁体   English

如何使用基于 js 的 swtich 为变量分配另一个值

[英]How to assign another value to a variable using js based swtich

I need it to change link6/linkr/linkd to "t" using switch based on "checked", switch is working visual correctly but it doesn`t change link6 to "t"我需要它使用基于“已检查”的开关将 link6/linkr/linkd 更改为“t”,开关在视觉上工作正常,但它不会将 link6 更改为“t”

Anyway switch doesn`t change links to "t"无论如何 switch 不会将链接更改为“t”

Using debugger i find out that it doesn`t want to trigger, help me make it triggerable使用调试器我发现它不想触发,帮我让它可触发

var link6 = "r";
var linkr = "r";
var linkd = "r";

    function download74() {
document.getElementById('link1' + link6).click();
}   
function download143() {
document.getElementById('link2' + linkr).click();
}
    function download198() {
document.getElementById('link3' + linkd).click();
}
function myFunction74() {
  if (confirm("Вы собираетесь скачать 74 devblog (RustUP)!")) {
    download74();
  } else {
    return 0;
  }
}
function myFunction143() {
  if (confirm("Вы собираетесь скачать 143 devblog (Immortal)!")) {
    download143();
  } else {
    return 0;
  }
}
function myFunction198() {
  if (confirm("Вы собираетесь скачать 198 devblog (TRINITY)!")) {
    download198();
  } else {
    return 0;
  }
}

var value = 0;
    var checkbox1 = document.getElementById("checkbox1");
    checkbox1.checked = value;
    document.getElementById("checkbox1").addEventListener("change", function(element){
            if (element.checked) {
      link6 = "t";
    } else {
      link6 = "r";
    }
    });
    var checkbox2 = document.getElementById("checkbox2");
    checkbox2.checked = value;
    debugger;
    document.getElementById("checkbox2").addEventListener("change", function(element){
            if (element.checked) {
            conslose.log("works");
            linkr = "t";
    } else {
    console.log("dont");
      linkr = "r";
    }
    });
    var checkbox3 = document.getElementById("checkbox3");
    checkbox3.checked = value;
    document.getElementById("checkbox3").addEventListener("change", function(element){
            if (element.checked) {
            linkd = "t";
    } else {
      linkd = "r";
    }
    });
    ```

You are using "if (checkbox1.checked)" in your anonymous change function, but when this code is triggered, the checkbox1 element no longer exists.您在匿名更改函数中使用“if (checkbox1.checked)”,但是当触发此代码时,checkbox1 元素不再存在。 You need to do "if (this.checked)" or "if (element.checked)" .您需要执行 "if (this.checked)" 或 "if (element.checked)" 。

I did an example.我做了一个例子。 Take a look if works for you:看看是否适合您:

var checkbox2 = document.getElementById("checkbox2");
checkbox2.checked = value;

checkbox2.addEventListener('change', (event) => {
  if (event.target.checked) {
   linkr = "t";
  } else {
    linkr = "r";
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM