在 dashcode 中以编程方式更改复选框的状态
好的,所以我尝试在仪表板代码中以编程方式更改复选框的状态。我尝试过:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Dashboard Widget 仅在 WebKit 技术上运行,因此对 Safari 有效的代码在 Dashcode 中也应该有效。以下任一方法都应该有效:
它们不工作的事实表明您的代码中的其他地方存在问题。我会检查该行
正确地将元素分配给
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:
The fact that they are not working indicates there is a problem elsewhere in your code. I would check the line
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).当我写这个答案时,这个问题已经一个月了。它可能已经解决了,但无论如何我想补充一点,如果您使用 Dashcode,复选框部分是一个 div,其中包含一个标签和一个输入,这是“真正的”复选框。
如果您检查在 Safari 中加载的 html,您会注意到“checkbox”是元素的类型。
因此,更改复选框状态的正确方法是,假设“输入”是它的 id(但它可以附加一个默认数字):
或您想要使用的任何方法。
这里的要点是你试图改变另一个 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):
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!
这个问题已经存在有一段时间了。无论如何,以下内容对我们有用:
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.
我不知道你用的是哪种浏览器,但是当我在FF 3.6上测试时,它可以工作。
就这样写:
while:
或这样写
I don't know which browser you used, but when I tested on FF 3.6, it works.
just put like this:
while:
or write like that
也许:
那么:
Maybe:
Then:
我创建一个表格、行和单元格,并在其中创建一个复选框。
我可以抓住第一个输入对象并将检查状态设置为 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.
这条线有问题,应该是
there is a problem with this line, it should be