我正在尝试使用jquery创建动态复选框。但显示一些错误未捕获的SyntaxError:意外的令牌ILLEGAL
我试图从代码中删除所有空格。但即使它显示相同的错误。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style>
#container input {
display: block;
}
.new-element.active {
color: red;
}
</style>
<script>
$(document).ready(function () {
$("#createCheckBox").click(function (e) {
var lbl = prompt("Enter label", "");
if (lbl != null) {
var name = "
<div class='new-element'>
<input class='chk' type='checkbox' id='chk' name='chk' />
<label for='chk'>" + lbl + "</label>
</div>";
var result = true;
$("#holder").find("input[type=checkbox]").each(function (index, value) {
console.log('Entered here');
if ($($(value).closest("div").children()[1]).text() == lbl) {
result = false;
alert(lbl + ' already exists');
return;
}
});
if (result)
$("#holder").append(name);
}
});
$("#delete").click(function () {
$('.new-element input:checked').parent().remove();
});
});
</script>
</head>
<body>
<div id="container">
<input type="button" id="createCheckBox" value="Add CheckBox" style="" />
<input type="button" id="delete" value="Delete" style="" />
<input type="button" id="selectall" value="selectall" style="" />
</div>
<div id="holder"></div>
</body>
</html>
最佳答案
两件事情 :
1)首先,在结尾处有一个奇怪的字符
if (result) {
$("#holder").append(name);
}
}); <-- here
2)您的功能未正确关闭第49行,请更换:
}); by }});