2016-04-22 48 views
0

动态生成的ID,并指定标记

$(document).ready(function() { 
 
    $("body").tooltip({ 
 
    selector: '[data-toggle=tooltip]' 
 
    }); 
 
}); 
 

 
var unique_id=0; 
 
$('#update').on('click', function() { 
 
    if ($('#myText').val().length == 0) { 
 
    alert('Are you Kidding ? ;)') 
 
    return 
 
    } 
 
    unique_id++; 
 
    $('#result').append('<li>' + '<input id="checkboxFourInput" type="checkbox" class="todo-item" /> <label for="checkboxFourInput"></label>' + $('#myText').val() + '<i class="glyphicon glyphicon-remove todo-item-del"></i>' + '</li>'); 
 
    // $('#myText').val("").attr("disabled", "disabled"); 
 

 
    // update value 
 
    $('#myText').val('').change(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}); 
 
$('#edit').on('mouseup', function() { 
 
    console.log($('#myText')); 
 
    $('#myText').prop("disabled", false).focus().select(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}) 
 

 
$('.hide').on("click", function() { 
 
    $('.hide').removeClass("show"); 
 
}); 
 

 
$('#result ').on('click', 'li .glyphicon-remove', function() { 
 
    // alert('Clicked'); 
 
    $(this).closest('li').remove(); 
 
});
.editable_text{ 
 
    margin: 20px; 
 
    /*padding: 15px;*/ 
 
    border: 1px solid #ccc; 
 
    width: 300px; 
 
    border-radius: 5px; 
 
    background: white; 
 
    display: inline-block; 
 
    text-align: left; 
 
} 
 
ul#result{ 
 
    padding: 0; 
 
    margin: 0; 
 
    position: relative; 
 
    top: -40px; 
 
    border-top: 1px solid #ccc; 
 
} 
 
#result li { 
 
    padding: 15px 25px; 
 
    border-bottom: 1px solid #ccc; 
 
    list-style: none; 
 
    font-size: 18px; 
 
    font-weight: 100; 
 
    color: #555; 
 
    letter-spacing: 1px; 
 
    width: 100%; 
 
} 
 
#myText{ 
 
    margin: 0; 
 
    padding: 20px 30px 20px 40px; 
 
    border: transparent; 
 
    border-bottom: 1px solid #ccc; 
 
    position: relative; 
 
    /*left: -16px;*/ 
 
    width: 100%; 
 
    top: -40px; 
 
} 
 

 
.btns{ 
 
    width:100%; 
 
    position: relative; 
 
    padding: 20px 0; 
 
} 
 
.plus { 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    position: relative; 
 
    right: 2px; 
 
    z-index: 1; 
 
} 
 
.okay{ 
 
    float: right; 
 
    position: relative; 
 
    right: -2px; 
 
    top: 0px; 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    z-index: 1; 
 
} 
 
.gray_text{ 
 
    opacity: 0.2; 
 
    color: #222; 
 
} 
 
.show{ 
 
    opacity: 1; 
 
    color: blue; 
 
} 
 
input[type=checkbox] { 
 
    visibility: hidden; 
 
} 
 
#result label { 
 
    cursor: pointer; 
 
    position: absolute; 
 
    width: 24px; 
 
    height: 24px; 
 
    top: 15px; 
 
    left: 15px; 
 
    background: #fff; 
 
    border: 1px solid #4AADF1; 
 
    border-radius: 100%; 
 
} 
 
#result label:focus{ 
 
    background: #4AADF1; 
 
} 
 
#result label:after { 
 
    opacity: 0.2; 
 
    content: ''; 
 
    position: absolute; 
 
    width: 12px; 
 
    height: 5px; 
 
    left: 5px; 
 
    top: 7px; 
 
    background: transparent; 
 
    border: 2px solid white; 
 
    border-top: none; 
 
    border-right: none; 
 
    -webkit-transform: rotate(-45deg); 
 
    -moz-transform: rotate(-45deg); 
 
    -o-transform: rotate(-45deg); 
 
    -ms-transform: rotate(-45deg); 
 
    transform: rotate(-45deg); 
 
} 
 

 
/** 
 
* Create the hover event of the tick 
 
*/ 
 
#result label:hover::after { 
 
    opacity: 0.5; 
 
} 
 

 
/** 
 
* Create the checkbox state for the tick 
 
*/ 
 
#result input[type=checkbox]:checked + label:after { 
 
    opacity: 1; 
 
} 
 
#result input[type=checkbox]:checked + label{ 
 
    background: #4AADF1; 
 
} 
 
.todo-item-del{ 
 
    text-align: right; 
 
    float: right; 
 
}
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
<!-- Google JS --> 
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
 
<!-- Latest compiled and minified JavaScript --> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<div class="box"> 
 
    <div class="editable_text"> 
 
    <div class="btns"> 
 
     <a id="edit" class="pull-left plus" data-toggle="tooltip" data-placement="bottom" title="Click to Add"> 
 
     <i class="glyphicon glyphicon-plus"></i> 
 
     </a> 
 

 
     <a id="update" class="gray_text okay" data-toggle="tooltip" data-placement="right" title="Click to Okay"> 
 
     <i class="glyphicon glyphicon-ok"></i> 
 
     </a> 
 
    </div> 
 
    <input type='text' id="myText" placeholder="Add new goals"/> 
 
    <ul id="result"></ul> 
 
    </div> 
 
</div>

您好,

我想实现一个待办事项列表中用于自己的管理控制台,在那里我得到了strucked,在这里,我在我的待办事项列表中追加了一个li作为一个项目,这是第一次它工作正常,我得到checkbox这也正在工作,但是当创建第二个待办事项时,它正在出错,并且复选框不起作用。

这里,我是想,但力的代码获得成功

$('#result').append('<li>' + '<input "$(this).attr("id",$(this).attr("id")+"_"+(unique_id))" type="checkbox" class="todo-item" /> <label for="(unique_id)"></label>' + $('#myText').val() + '<i class="glyphicon glyphicon-remove todo-item-del"></i>' + '</li>');

以下是小提琴link

回答

1

2个问题:需要基于

  • id的复选框on unique_id
  • #result label { ... top: 15px; ... }导致每个复选框固定在相同的y位置。

$(document).ready(function() { 
 
    $("body").tooltip({ 
 
    selector: '[data-toggle=tooltip]' 
 
    }); 
 
}); 
 

 
var unique_id = 0; 
 
$('#update').on('click', function() { 
 
    if ($('#myText').val().length == 0) { 
 
    alert('Are you Kidding ? ;)') 
 
    return 
 
    } 
 
    unique_id++; 
 
    $('#result').append('<li>' + '<input id="checkbox'+unique_id+'Input" type="checkbox" class="todo-item" /> <label for="checkbox'+unique_id+'Input"></label>' + $('#myText').val() + '<i class="glyphicon glyphicon-remove todo-item-del"></i>' + '</li>'); 
 
    // $('#myText').val("").attr("disabled", "disabled"); 
 

 
    // update value 
 
    $('#myText').val('').change(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}); 
 
$('#edit').on('mouseup', function() { 
 
    console.log($('#myText')); 
 
    $('#myText').prop("disabled", false).focus().select(); 
 
}); 
 

 
$('#myText').on("focus click", function() { 
 
    $('#update').removeClass("gray_text"); 
 
    $('#update').addClass("show"); 
 
}) 
 

 
$('.hide').on("click", function() { 
 
    $('.hide').removeClass("show"); 
 
}); 
 

 
$('#result ').on('click', 'li .glyphicon-remove', function() { 
 
    // alert('Clicked'); 
 
    $(this).closest('li').remove(); 
 
});
.editable_text { 
 
    margin: 20px; 
 
    /*padding: 15px;*/ 
 
    border: 1px solid #ccc; 
 
    width: 300px; 
 
    border-radius: 5px; 
 
    background: white; 
 
    display: inline-block; 
 
    text-align: left; 
 
} 
 
ul#result { 
 
    padding: 0; 
 
    margin: 0; 
 
    position: relative; 
 
    top: -40px; 
 
    border-top: 1px solid #ccc; 
 
} 
 
#result li { 
 
    padding: 15px 25px; 
 
    border-bottom: 1px solid #ccc; 
 
    list-style: none; 
 
    font-size: 18px; 
 
    font-weight: 100; 
 
    color: #555; 
 
    letter-spacing: 1px; 
 
    width: 100%; 
 
} 
 
#myText { 
 
    margin: 0; 
 
    padding: 20px 30px 20px 40px; 
 
    border: transparent; 
 
    border-bottom: 1px solid #ccc; 
 
    position: relative; 
 
    /*left: -16px;*/ 
 
    width: 100%; 
 
    top: -40px; 
 
} 
 
.btns { 
 
    width: 100%; 
 
    position: relative; 
 
    padding: 20px 0; 
 
} 
 
.plus { 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    position: relative; 
 
    right: 2px; 
 
    z-index: 1; 
 
} 
 
.okay { 
 
    float: right; 
 
    position: relative; 
 
    right: -2px; 
 
    top: 0px; 
 
    padding: 0 20px; 
 
    cursor: pointer; 
 
    z-index: 1; 
 
} 
 
.gray_text { 
 
    opacity: 0.2; 
 
    color: #222; 
 
} 
 
.show { 
 
    opacity: 1; 
 
    color: blue; 
 
} 
 
input[type=checkbox] { 
 
    visibility: hidden; 
 
} 
 
#result label { 
 
    cursor: pointer; 
 
    position: absolute; 
 
    width: 24px; 
 
    height: 24px; 
 
    /*top: 15px;*/ 
 
    left: 15px; 
 
    background: #fff; 
 
    border: 1px solid #4AADF1; 
 
    border-radius: 100%; 
 
} 
 
#result label:focus { 
 
    background: #4AADF1; 
 
} 
 
#result label:after { 
 
    opacity: 0.2; 
 
    content: ''; 
 
    position: absolute; 
 
    width: 12px; 
 
    height: 5px; 
 
    left: 5px; 
 
    top: 7px; 
 
    background: transparent; 
 
    border: 2px solid white; 
 
    border-top: none; 
 
    border-right: none; 
 
    -webkit-transform: rotate(-45deg); 
 
    -moz-transform: rotate(-45deg); 
 
    -o-transform: rotate(-45deg); 
 
    -ms-transform: rotate(-45deg); 
 
    transform: rotate(-45deg); 
 
} 
 
/** 
 
* Create the hover event of the tick 
 
*/ 
 

 
#result label:hover::after { 
 
    opacity: 0.5; 
 
} 
 
/** 
 
* Create the checkbox state for the tick 
 
*/ 
 

 
#result input[type=checkbox]:checked + label:after { 
 
    opacity: 1; 
 
} 
 
#result input[type=checkbox]:checked + label { 
 
    background: #4AADF1; 
 
} 
 
.todo-item-del { 
 
    text-align: right; 
 
    float: right; 
 
}
<!-- Latest compiled and minified CSS --> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
 
<!-- Google JS --> 
 
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
 
<!-- Latest compiled and minified JavaScript --> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 

 
<div class="box"> 
 
    <div class="editable_text"> 
 
    <div class="btns"> 
 
     <a id="edit" class="pull-left plus" data-toggle="tooltip" data-placement="bottom" title="Click to Add"> 
 
     <i class="glyphicon glyphicon-plus"></i> 
 
     </a> 
 

 
     <a id="update" class="gray_text okay" data-toggle="tooltip" data-placement="right" title="Click to Okay"> 
 
     <i class="glyphicon glyphicon-ok"></i> 
 
     </a> 
 
    </div> 
 
    <input type='text' id="myText" placeholder="Add new goals" /> 
 
    <ul id="result"></ul> 
 
    </div> 
 
</div>

+0

谢谢@Fabricator –

1

一个简单的方法来创建JS一个独特的ID与Date对象和的getTime(),它1970年00:00:00 UTC一月返回自1毫秒数。

new Date().getTime(); // 1461348496454 

但是,看起来这不是标签/复选框看起来丢失的原因。他们在那里;他们只是在同一个地方,因为他们是position: absolute。您只需将position: relative添加到您的li即可。

在附注上,我建议使用某种类型的客户端模板来完成这种任务。它可以更容易地发现问题并保持清洁。如果你没有在你的网站上做这么多,把手可能会矫枉过正。你可能想看看Underscore这样简单的事情。这将是一个简单而快速的实施。

你只需要改变几件事情要使用下划线模板。首先包括在你的页面下划线,然后添加一个脚本标签的模板部分:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"/> 
<script type="text/template" class="template"> 
    <li> 
     <input id="<%- uid %>" type="checkbox" class="todo-item"> 
     <label for="<%- uid %>"></label><%- labelText %><i class="glyphicon glyphicon-remove todo-item-del"></i> 
    </li> 
</script> 

更改你的点击处理程序来构建模板的数据对象,并使用模板来创建你的HTML字符串:

// _.template returns a function that is used to produce html string using whatever data you pass. 
var todoTemplate = _.template($('script.todoTemplate').html()); 

$('#update').on('click', function() { 
    var $myText, labelText, uid, templateData, html; 
    $myText = $('#myText'); 
    labelText = $myText.val(); 
    if (labelText.length === 0) { 
     alert('Are you Kidding ? ;)'); 
     return; 
    } 
    // create the unique id for the label 
    uid = new Date().getTime(); 
    // create the template data object 
    templateData = { 
     uid: uid, 
     labelText: labelText 
    }; 
    // use your template function to produce the html string you will append 
    html = todoTemplate(templateData); 

    $('#result').append(html); 

    // update value 
    $myText.val('').change(); 
}); 

我创建了一个的jsfiddle来测试它,如果你想看到一个工作示例: https://jsfiddle.net/n7bwymno/1/