使用 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>
如您所见,我尝试了两种方法来禁用点击事件:
方法 1: 我尝试过以下方法: how to unbind all event using jquery
结果:作品,解绑来自具有删除答案类别的锚点的点击事件,但是:
1) 它仅停用具有删除答案类别的锚点。我更愿意在 servlet 执行其操作时禁用所有 链接。
2)我无法(或者我不知道如何)重新启用链接。
方法2:我尝试了以下方法:如何动态地使用 jQuery 启用/禁用链接?
结果:它适用于普通锚点,但不适用于具有删除答案类的锚点。
两者似乎不兼容,所以我非常感谢一些帮助。
注意:还尝试更改执行此操作的类: $('.delete-answer').addClass('delete-disabled').removeClass('delete-answer');
它更改了类并仅将锚点保留为禁用删除的类,但是当我再次单击它们时,它们仍在删除消息,我不知道为什么:/
My question is related about disabling links/click events with jQuery, and it's probably easier than it seems to me. I commented the important code to make it easier.
I have the following code in a .js file:
$('.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.');
});
And In my HTML I have some list items like these:
<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>
As you can see, I tried two methods to disable the click event:
Method 1: I tried the following method: how to unbind all event using jquery
Result: It works, unbinding the click event from the anchors with delete-answer class, but:
1) It only deactivate the anchors with delete-answer class. I will prefer to disable all links while the servlet is doing it's stuff.
2) I can't (or I don't know how to) re-enable the links.
Method 2: I tried the following method: How do I dynamically enable/disable links with jQuery?
Result: It works for normal anchors, but not for the anchors with class delete-answer.
Both seem incompatible, so I'd really appreciate some help.
Note: also tried to change the class doing this: $('.delete-answer').addClass('delete-disabled').removeClass('delete-answer');
It changes the class and leaves the anchors only with delete-disabled class, but when I click them again, they're still deleting the message and I don't know why :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将所有代码包装在一个函数中并使用一个标志。
将其添加到顶部:
<前><代码>(函数() {
在底部添加此内容:
<前><代码>})();
在上面的顶行下方添加:
在点击处理程序的顶部:
将您的
帖子
更改为:将所有内容放在一起:
如果您感觉足够偏执,您可以使用计数器而不是布尔值,但概念是相同的。
Wrap all of that code in a function and use a flag.
Add this at the top:
Add this at the bottom:
Just under the top line above, add:
In your click handler, right at the top:
Change your
post
to:Bringing that all together:
If you're feeling sufficiently paranoid, you might use a counter rather than a boolean, but the concept is the same.
使用以下代码来完成此操作:
Use the following code to do it:
定义一个单独的变量来跟踪您的删除状态:
此外,如果锚点实际上不包含 URL,则不需要在锚点内添加
href
属性。只需将其完全删除即可。Define a separate variable that keeps track of your deleting state:
Also, you don't need an
href
attribute inside the anchor if it doesn't actually contain a URL. Just remove it altogether.另一个简单的解决方案就是 .hide() / .show() 你的点击元素。
Another simple solution is just .hide() / .show() your click element.
由于我无法发表评论,这是 Noob 在 Darm 帖子上提出的问题的解决方案 (LINK)。
我相信如果两个 .bind 用于同一元素,则页面将使用首先实现的 .bind 。因此,如果您想将设置从 FALSE 更改为 TRUE,则必须先.unbind它。要重新启用单击,请将后一个代码添加到您想要重新启用它的任何功能/事件中。
禁用
$('a').bind('click', true);
重新启用
还,我不知道为什么,除非我包含“jquery-ui.js”,否则设置回 TRUE 将不起作用。
希望这有帮助
Since I can't leave a comment, this is solution for Noob's question on Darm's post (LINK).
I believe the page uses whichever .bind was first implemented if two .binds are used for the same element. So you have to .unbind it first if you want to change the setting from FALSE to TRUE. To re-enable the click add the latter code to whichever function/event you would want to re-enable it.
DISABLE
$('a').bind('click', true);
RE-ENABLE
ALSO, I'm not sure why, setting back to TRUE wouldn't work unless I included "jquery-ui.js".
Hope this helps