动态地向列表中添加复选框和文本
Add a check box and text to an li dynamically
我正在尝试动态添加一个复选框和文本到一个li
我的代码
var statusList = document.createElement('ul');
for (var item in object) {
var option = document.createElement('li');
var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
//these are the things I have tried. I can get the title or the checkbox
but not both
//option.appendChild(checkbox); this adds the checkbox but not the text
//option.appendChild(object[item]); this throws an error
//option.text = checkbox + object[item]; this populates an empty li
//option.innerHTML = checkbox + object[item]; this populates the
//checkbox but not as an element. in other words it will say
//[object HTMLInputElement] + text
//option.text = object[item]; this populates the text but not checkbox
option.value = item;
statusList.appendChild(option);
}
请不要给出jquery的答案。只有javascript
从注释扩展:
Text节点也是一个节点,所以你可以像普通元素一样附加它:
option.appendChild(checkbox);
option.appendChild(document.createTextNode("TEXT"));
MDN: document.createTextNode
MDN: Element.appendChild
相关文章:
- 复选框与文本内联-JS问题
- 根据文本值禁用复选框
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- Javascript选中/取消选中所有复选框并将值写入文本区域
- 获取文本框中所有选定复选框的值
- 动态选中文本区域中现有项目的复选框组
- 选中复选框时更改文本框值
- 单击每个表格行中的复选框时启用文本框
- jQuery表 - 如何根据选中的复选框在单元格中写入文本
- 在 AngularJs 中触发文本框的复选框
- 基于复选框 Jquery 更新文本框
- 根据复选框选择 jQuery 更新文本框
- 我无法禁用带有复选框的文本框
- 从选中的复选框中获取所有值,并将其放入文本区域
- 如何使用选中的复选框文本自动填充输入文本字段
- 如何隐藏复选框文本
- 改变输入的火灾功能;使用jQuery标记(复选框/文本框)
- 选中复选框文本不变
- 我怎么能得到一个复选框文本在<光>标记,并在jQuery中的另一个标签中显示
- 可能添加CSS到一些JS样式禁用复选框文本