我的问题是关于使用jQuery禁用链接/点击事件,它可能比我看起来更容易。我评论了重要的代码以使其更容易。使用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>
正如你所看到的,我尝试了两种方法禁用Click事件:
方法1:我尝试以下方法:how to unbind all event using jquery
结果:它起作用,从具有删除答案类的锚点解除绑定点击事件,但是:
1)它仅使用delete-answer类停用锚点。我宁愿要禁用所有的链接,而servlet正在做它的东西。
2)我不能(或者我不知道如何)重新启用链接。
方法2:我尝试以下方法:How do I dynamically enable/disable links with jQuery?
结果:它适用于正常锚,而不是与类中删除应答的锚。
两者似乎不相容,所以我想真的欣赏一些帮助。
注:也试图改变类这样做:$('.delete-answer').addClass('delete-disabled').removeClass('delete-answer');
它改变了类和叶锚只删除禁用类,但是当我再次点击他们,他们还在删除消息,我不知道为什么:/
这很难理解你在做什么。尝试使用jsfiddle.net重现您的问题,然后给我们一个链接到该代码。 – Deele
@Deele,我没有熟悉jsfiddle,但我会看看。谢谢:) – Noob
@Buitrako:你也可以看看http://jsbin.com,它就像jsFiddle,但可以在更广泛的浏览器中使用。 –