无法设置复选框已选中 | JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16855522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:14:56  来源:igfitidea点击:

Can't set a checkbox checked | JavaScript

javascriptforms

提问by Zarwag

I am currently discovering JavaScript and I have some problems... I want to to set a check box to checked by using Js, but it doesn't seem to work. :/

我目前正在发现 JavaScript,但我遇到了一些问题......我想使用 Js 设置一个复选框来检查,但它似乎不起作用。:/

Here is the HTML code:

这是 HTML 代码:

<td><input id="jm" type="checkbox" onClick="check(1)"></td>
<td><input id="conference" type="checkbox" name="casmaret"></td>

Here is the Js code:

这是Js代码:

 function check(valeur)

     switch (valeur)
     {
         case 1:
                  document.getElementById("jm").checked = true;
                  document.getElementById("conference").checked = true;
                  break;
      }
 }

I simplified it, but the fact is, I want to check some other checkbox by checking one checkbox. When I look over the debugger in Chrome, the function executes, but it doesn't change anything...

我简化了它,但事实是,我想通过选中一个复选框来选中其他复选框。当我查看 Chrome 中的调试器时,该函数会执行,但它不会改变任何东西......

Any ideas?

有任何想法吗?

Thanks

谢谢

回答by Devator

You are missing a {after your function check(valeur):

你错过了{你的function check(valeur)

function check(valeur)
{
     switch (valeur)
     {
         case 1:
                  document.getElementById("jm").checked = true;
                  document.getElementById("conference").checked = true;
                  break;
     }
}