JSFiddle - React, Tailwind, and code Playground
HTML
<div id="todoMain">
<input id="content" />
<button id="add">add</button>
<ul id="todoList">
<li class="item">
<input type="checkbox" class="check"/>
<span class="item-content"> tv </span>
<button class="del">x</button>
<button class="edit">edit</button>
</li>
</ul>
<div id="stat">
<span>已完成: <i class="finished">{finished}</i></span>
<span>未完成: <i class="unfinished">{finished}</i></span>
<span>总量: <i class="total">{finished}</i></span>
</div>
</div>
<div id="edit-dialog" style="display:none">
<br />
<input class="content" />
<button class="ok"> OK </button>
</div>
JavaScript
$(function(){
var ndContent = $("#content");
var ndList = $("#todoList");
var ndMain = $("#todoMain");
$("#add").click(function(){
var content = ndContent.val();
$(['<li class="item">',
'<input type="checkbox" class="check"/>',
'<span class="item-content">',
content,
'</span>',
'<button class="del">x</button>',
'<button class="edit">edit</button>',
'</li>'
].join('')
).appendTo(ndList);
updateStat();
});
ndList.on('click', '.del', function(){
$(this).parents('.item').remove();
});
updateStat();
//以下是新加的
ndList.on("click", ".check", function(){
updateStat();
});
ndList.on('click', '.edit', function(){
var item = $(this).parents('.item').find('.item-content');
var content = item.html();
ndMain.hide();
$("#edit-dialog .content").val(content);
$("#edit-dialog").show();
$("#edit-dialog .ok").one('click', function(){
var content = $("#edit-dialog .content").val();
item.html(content);
$("#edit-dialog").hide();
ndMain.show();
});
});
function updateStat(){
var finished = 0;
var totalCount = 0;
var unfinished = 0;
var checkList = ndList.find('.check');
checkList.each(function(index, checkbox) {
totalCount++;
console.log(checkbox);
if (checkbox.checked) finished++;
});
unfinished = totalCount - finished;
$(".finished").html(finished);
$(".unfinished").html(unfinished);
$(".total").html(totalCount);
}
});