【发布时间】:2012-02-05 01:53:56
【问题描述】:
我正在为 XML 文件中的每个项目创建一个复选框。每个文件的文本是"TRUE" 或"FALSE",我想为每个"TRUE" 创建一个选中的复选框,为每个"FALSE" 创建一个未选中的复选框。现在我的代码在 if 和 else 中重复,因为我想不出有条件地在该 JSON 中包含已检查参数的方法。有谁知道完成同样事情的更干燥的方法?
d = $(this)
if (d.text() === 'TRUE') {
$('<input>', {
className: 'checkbox',
type: 'checkbox',
id: d.attr('id'),
name: d.attr('name'),
checked: 'checked'
}).appendTo(td);
}
else {
$('<input>', {
className: 'checkbox',
type: 'checkbox',
id: d.attr('id'),
name: d.attr('name')
}).appendTo(td);
}
【问题讨论】:
-
如果符合您的要求,请尝试使用 .serialize()。