我有以下由foreach循环动态创建的html结构,并尝试通过从(ACTIVE HYPERLINK)访问它来删除整个元素。我尝试了许多不同的方式,但无法达到它。由于重复了整个块(ACTIVE HYPERLINK),我认为使用超链接的类名是没有意义的。我也尝试使用a.active但似乎没有用。
@foreach (var file in Model.FileAttachments)
{
<li class="aaa">
<div class="bbb">
<div class="ccc">
<div class="ddd">
<div class="eee">
<ul class="fff">
<li>
<a class="xxx" href="javascript:void(0);" data-id="@file.Id" data-toggle="confirmation" ></a> <!-- ACTIVE HYPERLINK -->
</li>
</ul>
</div>
</div>
</div>
</div>
</li>
}
<script>
$('.xxx').click(function (e) {
e.preventDefault();
$('[data-toggle="confirmation"]').confirmation('show');
});
$('[data-toggle="confirmation"]').confirmation({
//code omitted for brevity
onConfirm: function () { deleteRecord(); }, // Set event when click at confirm button
});
function deleteRecord() {
var $ctrl = $('.xxx');
$.ajax({
//code omitted for brevity
success: function (response, textStatus, XMLHttpRequest) {
if (response.success) {
ctrl.closest('.aaa').remove();
//or
$("a.active").closest('.jFiler-item').remove();
}
});
};
</script>
以下是我尝试的一些示例:
$("a.active").closest('.aaa').remove();
$(".xxx").closest('.aaa').remove();
$(this).data('.aaa')remove();
$("a.active").parents('li').eq(2)remove();
$(".xxx").parents('li').eq(2)remove();
有什么想法吗?
答案 0 :(得分:2)
更新回复
$('.xxx').click(function (e) {
var $this = $(this);
e.preventDefault();
$('[data-toggle="confirmation"]').confirmation({
//code omitted for brevity
onConfirm: function () {
deleteRecord($this); // send reference to delete method
}
});
$('[data-toggle="confirmation"]').confirmation('show');
});
...
function deleteRecord($ctrl) {
...
原始回复
如果您希望在单击<a>
元素时删除该块,则需要为click
事件分配处理程序:
$('.xxx').on('click', function(){
$(this).closest('.aaa').remove();
});
答案 1 :(得分:2)
在event.preventDefault()
元素的click
处使用事件延迟<a>
来阻止当前文档重新加载或重定向到其他资源的默认操作;将当前event.target
:this
传递给deleteRecord()
$(document).on("click", ".xxx", function (e) {
e.preventDefault();
var curr = $(this);
curr.confirmation('show')
.confirmation({
// pass `curr` to `deleteRecord()`
onConfirm: function () { deleteRecord(curr); }
})
});
function deleteRecord(el) {
$.ajax({
//code omitted for brevity
success: function (response, textStatus, jqxhr) {
if (response.success) {
el.closest('.aaa').remove();
}
});
};
答案 2 :(得分:0)
在 @ZoliSzabo 和 guest271314 的帮助下,最终的答案就像魅力一样。非常感谢您的帮助和建议......
@foreach (var file in Model.FileAttachments)
{
<li class="aaa">
<div class="bbb">
<div class="ccc">
<div class="ddd">
<div class="eee">
<ul class="fff">
<li>
<a class="xxx" href="javascript:void(0);"
data-id="@file.Id" data-toggle="confirmation" ></a> <!-- ACTIVE HYPERLINK -->
</li>
</ul>
</div>
</div>
</div>
</div>
</li>
}
<script>
var $ctrl = null; //define variables globally
var id = 0; //define variables globally
$('.icon-jfi-trash').bind('click', function (e) {
e.preventDefault();
$ctrl = $(this);
id = $(this).data('id');
$(this).find('[data-toggle="confirmation"]').confirmation('show');
});
$('[data-toggle="confirmation"]').confirmation({
//code omitted for brevity
onConfirm: function () { deleteRecord(); }
});
function deleteRecord() {
var token = $('[name=__RequestVerificationToken]').val();
$.ajax({
type: "POST",
url: '@Url.Action("DeleteRecord", "Controller")',
cache: false,
dataType: "json",
data: { __RequestVerificationToken: token , id: id },
success: function (response, textStatus, XMLHttpRequest) {
if (response.success) {
$ctrl.closest('.jFiler-item').remove();
}
}
});
}
<script>