使用 jQuery 禁用点击事件

发布于 2024-11-26 07:32:54 字数 2436 浏览 4 评论 0原文

我的问题与使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

像你 2024-12-03 07:32:54

将所有代码包装在一个函数中并使用一个标志。

  1. 将其添加到顶部:

    <前><代码>(函数() {

  2. 在底部添加此内容:

    <前><代码>})();

  3. 在上面的顶行下方添加:

    // 是否启用“删除答案”标志
    var deleteAnswerEnabled = true;
    
  4. 在点击处理程序的顶部:

    if (!deleteAnswerEnabled) {
        返回假;
    }
    
  5. 将您的帖子更改为:

    // 禁止删除答案
    删除应答启用 = false;
    $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
         // 现在我们已经完成了,再次启用它
        删除应答启用 = true;
    });
    

将所有内容放在一起:

// (1)
(function() {
    // (3)
    // Flag for whether "delete answer" is enabled
    var deleteAnswerEnabled = true;


    $('.delete-answer').click(function(event) {
        event.preventDefault();

        // (4)
        // Don't do it if we're disabled
        if (!deleteAnswerEnabled) {
            return false;
        }

        // 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();

        // (5)
        // Disable deleting answers while we're doing it
        deleteAnswerEnabled = false;
        $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
             // Enable it again now we're done
            deleteAnswerEnabled = true;
        });

        $('.output').empty();
        $('.output').append('Message deleted successfully.');

    });
// (2)
})();

如果您感觉足够偏执,您可以使用计数器而不是布尔值,但概念是相同的。

Wrap all of that code in a function and use a flag.

  1. Add this at the top:

    (function() {
    
  2. Add this at the bottom:

    })();
    
  3. Just under the top line above, add:

    // Flag for whether "delete answer" is enabled
    var deleteAnswerEnabled = true;
    
  4. In your click handler, right at the top:

    if (!deleteAnswerEnabled) {
        return false;
    }
    
  5. Change your post to:

    // Disable deleting answers while we're doing it
    deleteAnswerEnabled = false;
    $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
         // Enable it again now we're done
        deleteAnswerEnabled = true;
    });
    

Bringing that all together:

// (1)
(function() {
    // (3)
    // Flag for whether "delete answer" is enabled
    var deleteAnswerEnabled = true;


    $('.delete-answer').click(function(event) {
        event.preventDefault();

        // (4)
        // Don't do it if we're disabled
        if (!deleteAnswerEnabled) {
            return false;
        }

        // 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();

        // (5)
        // Disable deleting answers while we're doing it
        deleteAnswerEnabled = false;
        $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
             // Enable it again now we're done
            deleteAnswerEnabled = true;
        });

        $('.output').empty();
        $('.output').append('Message deleted successfully.');

    });
// (2)
})();

If you're feeling sufficiently paranoid, you might use a counter rather than a boolean, but the concept is the same.

游魂 2024-12-03 07:32:54

使用以下代码来完成此操作:

$('a').bind('click', false);

Use the following code to do it:

$('a').bind('click', false);
茶底世界 2024-12-03 07:32:54

定义一个单独的变量来跟踪您的删除状态:

var isDeleting = false; 

$('.delete-answer').click(function(event) {
   if (!isDeleting) {
      isDeleting = true;

      $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
          isDeleting = false;
      });      
   }
});

此外,如果锚点实际上不包含 URL,则不需要在锚点内添加 href 属性。只需将其完全删除即可。

Define a separate variable that keeps track of your deleting state:

var isDeleting = false; 

$('.delete-answer').click(function(event) {
   if (!isDeleting) {
      isDeleting = true;

      $.post("../app/ForumCampus", {action:action, idMsg:idMsg}, function(data) { 
          isDeleting = false;
      });      
   }
});

Also, you don't need an href attribute inside the anchor if it doesn't actually contain a URL. Just remove it altogether.

爱你不解释 2024-12-03 07:32:54

另一个简单的解决方案就是 .hide() / .show() 你的点击元素。

Another simple solution is just .hide() / .show() your click element.

执着的年纪 2024-12-03 07:32:54

由于我无法发表评论,这是 Noob 在 Darm 帖子上提出的问题的解决方案 (LINK)。

我相信如果两个 .bind 用于同一元素,则页面将使用首先实现的 .bind 。因此,如果您想将设置从 FALSE 更改为 TRUE,则必须先.unbind。要重新启用单击,请将后一个代码添加到您想要重新启用它的任何功能/事件中。

禁用

$('a').bind('click', true);

重新启用

$('a').unbind('click', false);
$('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

$('a').unbind('click', false);
$('a').bind('click', true);`

ALSO, I'm not sure why, setting back to TRUE wouldn't work unless I included "jquery-ui.js".

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文