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

发布于 2024-08-25 14:37:37 字数 240 浏览 5 评论 0原文

好的,所以我尝试在仪表板代码中以编程方式更改复选框的状态。我尝试过:

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

眼中杀气 2024-09-01 14:37:37

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

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

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

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

正确地将元素分配给 checkbox 变量。另外,检查“checkbox”元素的 id 是否有效且正确(不重复、没有拼写错误等)。

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

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 checkbox variable. Also, check the id of your "checkbox" element is valid and correct (not a duplicate, doesn't have a typo, etc).

掩耳倾听 2024-09-01 14:37:37

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

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

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

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

或您想要使用的任何方法。

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

希望有帮助!

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.

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

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):

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.

Hope it helps!

二货你真萌 2024-09-01 14:37:37
checkbox.setAttribute("checked", "checked"); // set
checkBox.removeAttribute("checked"); // remove
checkbox.setAttribute("checked", "checked"); // set
checkBox.removeAttribute("checked"); // remove
从﹋此江山别 2024-09-01 14:37:37

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

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

方式需要通过 div 包装器,它具有实际 ID(本例中的复选框)并设置输入的属性,即子节点 1。

正如前面的答案中所指出的,Dashcode 创建这些控件的 输入的实际“id”会有问题,因为您无法控制分配给节点的 id。例如,如果您有两个复选框,则第一个复选框将“input”作为子节点 1 的 id,第二个复选框将“input1”作为子节点 1 的 id,除非您在源中的某处使用“input”或“input1”作为 id。你的设计已经有了!

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

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

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.

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!

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

享受孤独 2024-09-01 14:37:37

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

checkbox.checked = false;

while:

checkbox = document.getElementById('blablabla');

或这样写

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

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

checkbox.checked = false;

while:

checkbox = document.getElementById('blablabla');

or write like that

document.getElementById('idhere').checked = false;
心欲静而疯不止 2024-09-01 14:37:37

也许:

checkbox.checked = "checked";

那么:

checkbox.checked = "unchecked";

Maybe:

checkbox.checked = "checked";

Then:

checkbox.checked = "unchecked";
心如狂蝶 2024-09-01 14:37:37
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;

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

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.

遥远的绿洲 2024-09-01 14:37:37
var checkbox = document.getElementById("checkbox"); 

这条线有问题,应该是

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

there is a problem with this line, it should be

var checkbox = document.getElementById('#checkbox");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文