如何在Internet Explorer中通过javascript正确创建和操作复选框?
我正在写一个网络应用程序。根据用户选择的某些选项,它会动态创建许多复选框输入元素。为了可用性,应该在选中状态下创建它们,用户将取消选中他们不想要的 我的代码在Firefox中运行良好。不幸的是,我必须瞄准IE 7.0。好了,我运气不好。以下是相关部分 这将在DIV框中为ID创建一个带有CboxBlock的复选框如何在Internet Explorer中通过javascript正确创建和操作复选框?,javascript,internet-explorer,dom,Javascript,Internet Explorer,Dom,我正在写一个网络应用程序。根据用户选择的某些选项,它会动态创建许多复选框输入元素。为了可用性,应该在选中状态下创建它们,用户将取消选中他们不想要的 我的代码在Firefox中运行良好。不幸的是,我必须瞄准IE 7.0。好了,我运气不好。以下是相关部分 这将在DIV框中为ID创建一个带有CboxBlock的复选框 function InsertCheckBox(name, appfk) { // Create the text box node. var tbox = document
function InsertCheckBox(name, appfk)
{
// Create the text box node.
var tbox = document.createElement('input');
// Set all the values.
tbox.type = "checkbox";
tbox.checked = "checked";
tbox.name = "cbox";
tbox.value = appfk;
// Next, we need a paragraph element to place it in.
var para = document.createElement('p');
// Text to place inside P
para.appendChild( document.createTextNode(name) );
// Append text box
para.appendChild(tbox);
// Attach to the CboxBlock
block = document.getElementById("CboxBlock");
block.appendChild( para );
}
在Firefox中,这项功能可以立即运行。复选框已选中。在IE中,它们不是。因此,我在创建后为fire添加了另一个函数:
function SetCheckboxes()
{
block = document.getElementById("CboxBlock")
//cboxes = document.getElementsByName("cbox");
cboxes = block.childNodes;
for (ind in cboxes)
{
box = cboxes[ind];
box.checked = "checked";
}
}
我发现了一个愚蠢的bug,getElementsByName没有返回任何东西,但这仍然没有改变任何东西。文本框保持不变。我甚至试着把它改成box.checked=true,就像我在一些地方看到的那样,但这仍然没有改变它
有人能看出我在哪里犯了错误吗?有没有其他方法可以让我在IE中操纵复选框?感谢您提供的任何信息。我相信IE将其作为属性而不是属性访问
box.setAttribute('checked', 'checked');
在IE中非常好用,我已经用了很长时间了。好吧,我又换了一次,这次它现在可以用了。我不知道上次我试的时候为什么不起作用。谢谢