这周刚开始学习jQuery。
我目前正在开发一个基本的“待办事项”应用程序。我创建了一个表单,在其中输入了新的“待办事项”,然后通过添加按钮将其添加到表格中。一旦添加到表格中,添加的“待办事项”会伴随着两个按钮。一个用于删除“待办事项”(当前起作用),还有一个“完成”按钮,我希望能够删除“待办事项”行,但仍使“待办事项”文本清晰可见。
我在让完成按钮起作用时遇到了一些麻烦,我想知道是否有人可以伸出援手。作为一名学生,我还想知道是否有可能有一个复选框而不是一个在选中时会触发删除线的按钮。
另外,我有一个警报,当有重复的笔记时会触发,但它只在检测到第一个重复后才有效。我究竟做错了什么?
$(document).ready(function() {
$(".btn-primary").on("click", function(e) {
e.preventDefault();
//pull new note from html form input & trim for easier comparison
var newNote = $("#newNote").val().trim();
var isDuplicate = false;
$("td.note-td").each(function() {
if($(this).text().trim().toLowerCase() === newNote.toLowerCase()) {
isDuplicate = true;
return;
}
});
//after checking for the duplicates, if duplicate, alert! Then slideback
if(isDuplicate) {
$("#duplicateAlert").html("Again? We already noted this.");
$("p").slideUp(3000);
return;
}
//adding newNote into the exisiting table
var newRow = $("<tr>");
var noteTd = $("<td>").addClass("note-td").append(newNote);
var deleteBtn = $("<button>").addClass("btn btn-danger").append("Delete");
//Try to add a checkbox with the strike-thru like this?
var deleteTd = $("<td>").append(deleteBtn);
//add strikethru button on the add click, just like the delete button
var strikeBtn = $("<button>").addClass("btn btn-success").append("Done");
var strikeTd = $("<td>").append(strikeBtn);
newRow.append(noteTd).append(deleteTd).append(strikeTd);
$("tbody").append(newRow);
//empty newNote field
$("#newNote").val("").focus();
});
//functionality of delete button
$("table").on("click", ".btn-danger", function() {
if($("tr").length > 1) {
$(this).parent().parent().remove();
}
//Need functionality of doneBtn.
});
});
<body>
<!--create a container where the user will add their new 'To-do'-->
<div class="container-fluid">
<div class="row-fluid">
<div class="col-md-6">
<form class="form">
<div class="form-group">
<input type="text" class="form-control" id="newNote" placeholder="new to-do" autocomplete="off" />
<p id="duplicateAlert"></p>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<!--create a table that will archive the added 'To-do'-->
<div class="col-md-6">
<table class="table table-striped table-bordered table-hover table-condensed">
<thead>
<tr>
<th>Things to-do</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="do_function.js"></script>
</body>