这是我想要做的: for loop inside the append is not working
这是我的代码:
$('body').append(
$('<section>').append(
$('<form>')
.attr('class', "ac-custom ac-checkbox ac-cross")
.attr('autocomplete', "off")
.append(
$('<h2>')
.html(
"Your to do list to uniquely deploy cross-unit s:"
),
function() {
options = ["ONE", "TWO", "THREE", "FOUR"];
var $container = $('<ol></ol>');
$.each(options, function(val) {
$container.append($('<li>').append(
$("<input>").attr('id', "cb" +
val)
.attr('name', "cb" + val)
.attr('type', "checkbox"),
$("<label>").attr('for', "cb" +
val).append(
$('<span>').html(
"Quickly incentivize impactful actions"
)
)
));
})
return $('<ol>').html();
}
)
)
)
错误:
jquery-2.1.1.js:5089 Uncaught TypeError: elem.replace is not a function
我做错了什么?
答案 0 :(得分:2)
假设您只想附加h2
中的HTML和传递给append
的函数的输出,您可以将调用链接起来:
$('body').append(
$('<section>').append(
$('<form>')
.attr('class', "ac-custom ac-checkbox ac-cross")
.attr('autocomplete', "off")
.append(
$('<h2>')
.html(
"Your to do list to uniquely deploy cross-unit s:"
)
)
.append(
function() {
options = ["ONE", "TWO", "THREE", "FOUR"];
var $container = $('<ol></ol>');
$.each(options, function(val) {
$container.append($('<li>').append(
$("<input>").attr('id', "cb" +
val)
.attr('name', "cb" + val)
.attr('type', "checkbox"),
$("<label>").attr('for', "cb" +
val).append(
$('<span>').html(
"Quickly incentivize impactful actions"
)
)
));
})
return $container.html();
}
)
)
)
但难怪你在查看问题时遇到了麻烦。你应该把它拆分成更小,更易于管理的部分