我的问题与使用jQuery禁用链接/点击事件有关,这可能比我想象的要容易。我已经在代码中做了注释以使其更容易理解。
以下是我在.js文件中的代码:
$('.delete-answer').click(function(event) {
event.preventDefault();
// some actions modifying the tags
$('.output').closest('li').remove();
var idMsg = ...;
var action = ...;
var answers = ...;
$(this).closest('li').children('p').remove();
$(this).closest('.tr').before('<tr><td><div class="output">Deleting message...</div></td></tr>');
$(this).closest('.tr').remove();
// While the servlet is deleting the message, I want to disable the links
// but I can't, so my problem is just here
// METHOD 1
//$('a').unbind('click');
// METHOD 2
//$('a').bind('click', function(e){
// e.preventDefault();
//});
$.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) {
});
// METHOD 1
//$('a').bind('click');
// METHOD 2
//$('a').unbind('click');
$('.output').empty();
$('.output').append('Message deleted successfully.');
});
在我的 HTML 中,有一些像这样的列表项:
<li>
<p class="text">Some text.</p>
<table class="answer-details" style="width: 100%">
<tbody>
<tr class="tr">
<td style="width: 50%;">
<div class="msg-modification" display="inline" align="right">
<a id="modify" class="delete-answer" href="#">Delete</a>
</div>
</td>
</tr>
</tbody>
</table>
</li>
正如您所看到的,我尝试了两种方法来禁用点击事件: 方法1: 我尝试了以下方法:如何使用jQuery解除所有事件绑定 结果:它有效地从具有delete-answer类的锚点解除了点击事件,但是: 1)它只停用具有delete-answer类的锚点。在servlet执行其任务时,我更喜欢禁用所有链接。 2)我无法(或者我不知道如何)重新启用链接。 方法2: 我尝试了以下方法:如何使用jQuery动态启用/禁用链接? 结果:它适用于普通的锚点,但不适用于具有delete-answer类的锚点。
两者似乎不兼容,所以我真的需要一些帮助。
注意:我也试图通过更改类来实现此操作:
$('.delete-answer').addClass('delete-disabled').removeClass('delete-answer');
它更改了类并仅保留了delete-disabled类的锚点,但当我再次单击它们时,它们仍然会删除消息,我不知道为什么 :/