Javascript 在dashcode中以编程方式更改复选框的状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2488806/
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-08-23 00:36:43  来源:igfitidea点击:

Changing a checkbox's state programmatically in dashcode

javascriptcheckboxdashcode

提问by Daniel

Okay, so I'm trying to change a checkbox's state programmatically in dashcode. I've tried:

好的,所以我正在尝试在 dashcode 中以编程方式更改复选框的状态。我试过了:

var checkbox = document.getElementById("checkbox");

// I have tried all the following methods.
checkbox.checked = false;
checkbox.selected = false;
checkbox.value = false;

回答by Andy E

Dashboard Widgets just run on WebKit technologies, so code valid for Safari should also be valid in Dashcode. Either of the following should work:

Dashboard Widget 仅在 WebKit 技术上运行,因此对 Safari 有效的代码在 Dashcode 中也应该有效。以下任何一项都应该有效:

checkbox.checked = true;
checkbox.setAttribute("checked", "true");

The fact that they are not working indicates there is a problem elsewhere in your code. I would check the line

它们不工作的事实表明您的代码中的其他地方存在问题。我会检查线路

var checkbox = document.getElementById("checkbox");     

Correctly assigns an element to the checkboxvariable. Also, check the id of your "checkbox" element is valid and correct (not a duplicate, doesn't have a typo, etc).

正确地为checkbox变量分配一个元素。此外,请检查您的“复选框”元素的 id 是否有效且正确(不是重复的,没有拼写错误等)。

回答by Rogelio

This question is one month old as I write this answer. It was probably already solved, but in any case I would like to add that if you are using Dashcode, the Checkbox part is a div which contains one label and one input, this one being the "real" checkbox.

在我写这个答案时,这个问题已经有一个月了。它可能已经解决了,但无论如何我想补充一点,如果您使用的是 Dashcode,Checkbox 部分是一个 div,其中包含一个标签和一个输入,这个是“真正的”复选框。

If you inspect the html as it is loaded in Safari you will notice that "checkbox" is the type of the element.

如果您在 Safari 中加载 html 时检查它,您会注意到“复选框”是元素的类型。

Therefore the proper way to change the state of the checkbox would be, assuming "input" is its id (it could have a default number attached though):

因此,更改复选框状态的正确方法是,假设“输入”是它的 id(尽管它可以附加一个默认数字):

document.getElementById("input").checked="true";

or whichever method you want to use.

或您想使用的任何方法。

The main point here is that you were trying to change the state of another div.

这里的要点是您试图更改另一个 div 的状态。

Hope it helps!

希望能帮助到你!

回答by Jaanus

checkbox.setAttribute("checked", "checked"); // set
checkBox.removeAttribute("checked"); // remove

回答by thang

I don't know which browser you used, but when I tested on FF 3.6, it works. just put like this:

我不知道你用的是哪个浏览器,但是当我在 FF 3.6 上测试时,它可以工作。就像这样:

checkbox.checked = false;

while:

尽管:

checkbox = document.getElementById('blablabla');

or write like that

或者这样写

document.getElementById('idhere').checked = false;

回答by cameron

This question has been around a while. Regardless, the following works for us:

这个问题已经有一段时间了。无论如何,以下对我们有用:

checkbox.childNodes[1].checked = true; checkBox.childNodes[1].checked = false;

checkbox.childNodes[1].checked = true; checkBox.childNodes[1].checked = false;

As pointed out in a previous answer, the way Dashcode creates these controls you need to get past the div wrapper, which has the actual ID (checkbox in this example) and set the property for the input, which is child node 1.

正如在之前的回答中指出的那样,Dashcode 创建这些控件的方式需要通过 div 包装器,它具有实际的 ID(在此示例中为复选框)并设置输入的属性,即子节点 1。

Looking for the actual 'id' of the input would be problematic as you have no control over what id's are assigned to the node. For example if you have two checkboxes then the first one would have 'input' as the id for child node 1 and the second one 'input1', unless, of source you have used 'input' or 'input1' as an id somewhere in your design already!

查找输入的实际“id”会出现问题,因为您无法控制分配给节点的 id。例如,如果您有两个复选框,那么第一个复选框将使用“input”作为子节点 1 的 id,第二个将使用“input1”作为 id,除非您在源代码中使用了“input”或“input1”作为 id你的设计已经!

There might be another method but I have not found it yet.

可能还有另一种方法,但我还没有找到。

回答by Steve Morley

cell = row.insertCell(-1);
sel = document.createElement('input');
sel.setAttribute("type", "checkbox")
sel.setAttribute("name", "myCheckBox")
cell.appendChild(sel);
cell.getElementsByTagName('input')[0].checked = true;

I create a table, row then cell and create a checkbox within it. I can the grab hold of the first input object and set the checked status to true.

我创建一个表,行然后是单元格并在其中创建一个复选框。我可以抓住第一个输入对象并将检查状态设置为 true。

回答by Lucas Jones

Maybe:

也许:

checkbox.checked = "checked";

Then:

然后:

checkbox.checked = "unchecked";

回答by Iria

var checkbox = document.getElementById("checkbox"); 

there is a problem with this line, it should be

这条线有问题,应该是

var checkbox = document.getElementById('#checkbox");