Problem
Dynamically appended checkbox element does not appear checked despite setting the checked property state to true or "checked".
Browser
Internet Explorer
Example
The Javascript code:
Expand
|
Select
|
Wrap
|
Line Numbers
- var cb = document.createElement("input");
- cb.type = "checkbox";
- cb.name = "checkbox1";
- cb.id = "cbID";
- cb.checked = true;
- obj.appendChild(cb);
Solution
Use defaultChecked instead of checked:
Expand
|
Select
|
Wrap
|
Line Numbers
- cb.defaultChecked = true;
Alternative Solution
Set the checked state after appending the checkbox:
Expand
|
Select
|
Wrap
|
Line Numbers
- obj.appendChild(cb);
- cb.checked = true;
本文解决了一个在Internet Explorer浏览器中遇到的问题:通过JavaScript动态创建并设置为选中的复选框,在页面上显示时未被标记为选中状态。提供了两种解决方案。
772

被折叠的 条评论
为什么被折叠?



