Javascript 使用Jquery创建动态复选框
我正在尝试使用jquery创建动态复选框。但它显示了一些未捕获的SyntaxError错误:意外标记非法 我试图删除代码中的所有空格。但即使它显示出同样的错误Javascript 使用Jquery创建动态复选框,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试使用jquery创建动态复选框。但它显示了一些未捕获的SyntaxError错误:意外标记非法 我试图删除代码中的所有空格。但即使它显示出同样的错误 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <style> #container input
<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>
#容器输入{
显示:块;
}
.新元素{
颜色:红色;
}
$(文档).ready(函数(){
$(“#createCheckBox”)。单击(函数(e){
var lbl=prompt(“输入标签”);
如果(lbl!=null){
变量名称=”
“+lbl+”
";
var结果=真;
$(“#holder”).find(“输入[type=checkbox]”)。每个(函数(索引、值){
console.log('Entered here');
if($($(value).closest(“div”).children()[1]).text()==lbl){
结果=假;
警报(lbl+‘已存在’);
返回;
}
});
如果(结果)
$(“#持有人”)。附加(姓名);
}
});
$(“#删除”)。单击(函数(){
$('.new-element-input:checked').parent().remove();
});
});
两件事:
1) 首先,电影结尾有一个奇怪的角色
if (result) {
$("#holder").append(name);
}
}); <-- here
#容器输入{
显示:块;
}
.新元素{
颜色:红色;
}
$(文档).ready(函数(){
$(“#createCheckBox”)。单击(函数(){
var lbl=prompt(“输入标签”);
如果(lbl的类型!=“未定义”){
var name=“”+
" " +
“”+lbl+“”+
"";
var结果=真;
$(“#holder”).find(“输入[type=checkbox]”)。每个(函数(索引、值){
if($($(value).closest(“div”).children()[1]).text()==lbl){
结果=假;
警报(lbl+‘已存在’);
返回;
}
});
如果(结果)
$(“#持有人”)。附加(姓名);
}
})
$(“#容器”)。在('单击','删除',函数(){
$('.new-element-input:checked').parent().remove();
});
$(“#容器”)。在('单击','选择全部',函数(){
$(“#holder.new element”).find(“输入[type=checkbox]”)。每个(函数(i,值){
$(value).prop('checked',!$(value).is(':checked');
});
});
});
以下是HTML
<body>
<div id="container">
<input type="button" id="createCheckBox" value="Add CheckBox" />
<input type="button" id="delete" value="Delete" />
<input type="button" id="selectall" value="selectall" />
<input type="button" id="status" value="status" />
</div>
<div id="holder"></div>
</body>
下面是JS文件
$(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);
} // console.log(1);
});
$("#delete").click(function ()
{
$('.new-element input:checked').parent().remove();
});
$("#selectall").click(function ()
{
$('input[type="checkbox"]').attr('checked', true);
});
$("#status").click(function ()
{
debugger;
var checkBoxCount = $('input[type="checkbox"]').length;
var checkedStatus = $('input[type="checkbox"]:checked').length;
var totalCheckBox = checkBoxCount - checkedStatus;
alert("total check box : " + checkBoxCount + "checked check box : " + checkedStatus + "Unchecked checkBox :"+totalCheckBox);
});
});
$(文档).ready(函数(){
$(“#createCheckBox”)。单击(函数(e)
{
var lbl=prompt(“输入标签”);
如果(lbl!=null)
{
var name=“+lbl+”;
var结果=真;
$(“#holder”).find(“输入[type=checkbox]”)。每个(函数(索引、值)
{
console.log('Entered here');
if($($(value).closest(“div”).children()[1]).text()==lbl)
{
结果=假;
警报(lbl+‘已存在’);
返回;
}
});
如果(结果)
$(“#持有人”)。附加(姓名);
}//console.log(1);
});
$(“#删除”)。单击(函数()
{
$('.new-element-input:checked').parent().remove();
});
$(“#选择全部”)。单击(函数()
{
$('input[type=“checkbox”]”)。attr('checked',true);
});
$(“#状态”)。单击(函数()
{
调试器;
var checkBoxCount=$('input[type=“checkbox”]')。长度;
var checkedStatus=$('input[type=“checkbox”]:checked')。长度;
var totalCheckBox=checkBoxCount-checkedStatus;
警报(“总计复选框:“+checkBoxCount+”选中复选框:“+checkedStatus+”未选中复选框:“+totalCheckBox”);
});
});
<body>
<div id="container">
<input type="button" id="createCheckBox" value="Add CheckBox" />
<input type="button" id="delete" value="Delete" />
<input type="button" id="selectall" value="selectall" />
<input type="button" id="status" value="status" />
</div>
<div id="holder"></div>
</body>
$(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);
} // console.log(1);
});
$("#delete").click(function ()
{
$('.new-element input:checked').parent().remove();
});
$("#selectall").click(function ()
{
$('input[type="checkbox"]').attr('checked', true);
});
$("#status").click(function ()
{
debugger;
var checkBoxCount = $('input[type="checkbox"]').length;
var checkedStatus = $('input[type="checkbox"]:checked').length;
var totalCheckBox = checkBoxCount - checkedStatus;
alert("total check box : " + checkBoxCount + "checked check box : " + checkedStatus + "Unchecked checkBox :"+totalCheckBox);
});
});
<!DOCTYPE html>
<html>
<head>
<title>Mustache Dynamic Check Box</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/external/jquery-1.7.1.min.js"></script>
<script src="js/external/mustache.js"></script>
<script src="js/mustacheDynamic.js" type="text/javascript"></script>
</head>
<body>
<div id="container">
<input type="button" id="createCheckBox" value="Add CheckBox" />
<input type="button" id="delete" value="Delete" />
<input type="button" id="selectall" value="selectall" />
<input type="button" id="status" value="status" />
</div>
<div id="DynamicCheckBoxHolder">
</div>
<script id="dynamic-CheckBox-templete" type="text/templete">
<div class="{{eachDivClassName}}">
<input type="{{inputType}}" class="{{className}}" id="{{id}}" name="{{name}}"> <label for="{{id}}">{{UserEnteredCheckBoxLabel}}</label>
</div>
</script>
</body>
$(document).ready(function()
{
var eachDivClassName = "eachDiv";
var inputType = "checkbox";
var eachCheckBoxClassName = "eachCheckBox";
var eachCheckBoxClassId = "checkBoxId";
var eachCheckBoxName = "eachCheckBoxName"
$("#createCheckBox").on('click', function (e) {
var UserEnteredCheckBoxLabel = prompt("Enter label", "");
//var result = true;
if ((UserEnteredCheckBoxLabel != null) && (UserEnteredCheckBoxLabel != ""))
{
var result = true;
$("#DynamicCheckBoxHolder").find("input[type=checkbox]").each(function (index, value) {
console.log('Entered here');
if ($($(value).closest("div").children()[1]).text() === UserEnteredCheckBoxLabel)
{
result = false;
alert(UserEnteredCheckBoxLabel + ' already exists');
return;
}
});
if (result)
{
var dataObjectsForCreateCheckBox = {};
dataObjectsForCreateCheckBox.eachDivClassName = eachDivClassName;
dataObjectsForCreateCheckBox.UserEnteredCheckBoxLabel = UserEnteredCheckBoxLabel;
dataObjectsForCreateCheckBox.inputType = inputType;
dataObjectsForCreateCheckBox.className = eachCheckBoxClassName;
dataObjectsForCreateCheckBox.id = eachCheckBoxClassId;
dataObjectsForCreateCheckBox.name = eachCheckBoxName;
var template = $('#dynamic-CheckBox-templete').html();
var html = Mustache.to_html(template, dataObjectsForCreateCheckBox);
$("#DynamicCheckBoxHolder").append(html);
}
}
});
$("#delete").on('click', function()
{
$('.eachDiv input:checked').parent().remove();
});
$("#selectall").on('click', function()
{
$('input[type="checkbox"]').attr('checked', true);
});
$("#status").on('click', function (){
var totalCheckBoxCount = $('input[type="checkbox"]').length;
var checkedCheckBoxCount = $('input[type="checkbox"]:checked').length;
var unCheckedCheckBox = totalCheckBoxCount - checkedCheckBoxCount;
alert("total check box : " + totalCheckBoxCount + " checked check box : " + checkedCheckBoxCount + " Unchecked checkBox : " + unCheckedCheckBox);
});
});