1
我使用的是asp.net核心剃鬚刀引擎。我根據我的數據庫中有多少行填充我的html。我正在使用ajax從我的數據庫中刪除項目。它的工作原理,但我不知道如何隱藏剛刪除的數據。當我使用$(this).hide()
時,它隱藏了刪除按鈕。當我嘗試$(this).parent().hide()
時,我的所有數據都被隱藏了。jquery隱藏this.parent似乎隱藏一切
這裏是我的代碼
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.update').click(function(){
$(this).parent().submit(function(event){
event.preventDefault();
var $form = $(this);
url = $(this).attr("action");
term = $form.find("textarea[name='Description']").val();
var posting = $.post(url, { Description: term });
});
});
$('.delete').click(function(){ //This is my delete function
$(this).parent().submit(function(event){
event.preventDefault();
var $form = $(this);
url = $form.attr("action");
var posting = $.post(url);
$(this).parent().hide();
});
});
$('button').click(function(){
$('#New_Note').submit(function(event){
event.preventDefault();
var $form = $(this);
url = $form.attr("action");
term = $form.find("input[name='Note']").val();
console.log(term);
var posting = $.post(url, { Note: term });
posting.done(function(data) {
var content = $(data).find("#content");
console.log(content);
// <!--$("div").append(term);-->
var r= $('<p>term</p>');
});
});
});
});
</script>
@model ajaxNotes.Models.Home
<div class = "notes">
@{
if(ViewBag.Notes != null)
{
foreach(var note in ViewBag.Notes)
{
<p>@note.Note</p>
<form action="/delete/@note.Id" method="post" id="delete_note">
<input type="submit" name="submit" value="Delete" class='delete'/>
</form>
<form action="/AddDescription/@note.Id" method="post" class="update_note">
<label asp-for="Description" ></label>
<textarea name="Description" id="" cols="30" rows="10" class="Description">@note.Description</textarea>
<input type="submit" name="submit" class="update" value="Update"/>
</form>
}
}
}
</div>
<form asp-controller="Home" asp-action="AddNote" method="post" id="New_Note">
<label asp-for="Note" ></label>
<p><input asp-for="Note" class = "Note" name="Note"/> </p>
@{
if(ViewData["error"] != null)
{
<p>Please enter data for Notes</p>
}
}
<button type="submit">Add Note</button>
</form>
我建議不要使查詢變得複雜,只需添加一個封裝每個塊並將其刪除的div。這將導致未來的靈活性 –
@ A.J,謝謝。發佈你的答案,以便我可以給你信用 – Aaron