【发布时间】:2017-12-08 22:29:59
【问题描述】:
我有一个遗留页面,它使用大量原始 javascript(没有隐藏浏览器差异的 JS 库)。它动态地创建一个这样的复选框:
function createCheckbox(checkboxName, isChecked, onClickHandler)
{
var checkboxId = checkboxName + 'ChkBx';
var chkbx;
if(Ext.isIE)
{
// Following code does not work in IE10 when NOT using compatibility view
// Throws JS error. Worked in IE9.
//if (isChecked)
//{
// chkbx = document.createElement('<input id="' + checkboxId + '" type="checkbox" name="' + checkboxName + '" value="true" checked="checked" className="checkbox" />');
//}
//else
//{
// chkbx = document.createElement('<input id="' + checkboxId + '" type="checkbox" name="' + checkboxName + '" value="true" className="checkbox" />');
//}
// Following code does not work in IE10 when using compatibility view
// isChecked == true does not product checked checkbox
chkbx = document.createElement('input');
chkbx.id = checkboxId;
chkbx.name = checkboxName;
chkbx.value = 'true';
chkbx.type = 'checkbox';
chkbx.checked = isChecked;
chkbx.className = 'checkbox';
}
else
{ ... }
return chkbx;
}
我了解在 IE10 中,createElement() 不再接受复杂的字符串参数。这就是为什么我更改了上面的代码。我不明白为什么,它不尊重我将选中的属性设置为 true?应该如何指示复选框应该被选中?
【问题讨论】:
-
应该可以正常工作; “isChecked”的值究竟是什么?
标签: javascript checkbox internet-explorer-10 checked