4

我的问题与使用 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

结果:它起作用了,使用 delete-answer 类从锚点中解除 click 事件,但是:

1)它只停用带有删除答案类的锚。我宁愿在 servlet 执行此操作时禁用所有链接。

2) 我不能(或者我不知道如何)重新启用链接。

方法2:我尝试了以下方法:如何使用jQuery动态启用/禁用链接?

结果:它适用于普通锚点,但不适用于删除答案类的锚点。

两者似乎不兼容,所以我非常感谢一些帮助。


注意:也试图改变这个类:$('.delete-answer').addClass('delete-disabled').removeClass('delete-answer');

它更改了类并仅将锚点保留为禁用删除的类,但是当我再次单击它们时,它们仍在删除消息,我不知道为什么:/

4

5 回答 5

2

使用以下代码执行此操作:

$('a').bind('click', false);
于 2011-07-25T11:58:25.607 回答
2

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

  1. 在顶部添加:

    (function() {
    
  2. 在底部添加:

    })();
    
  3. 在上面的第一行下方,添加:

    // Flag for whether "delete answer" is enabled
    var deleteAnswerEnabled = true;
    
  4. 在您的点击处理程序中,位于顶部:

    if (!deleteAnswerEnabled) {
        return false;
    }
    
  5. 将您更改post为:

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

把这一切放在一起:

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

如果您感到足够多疑,您可能会使用计数器而不是布尔值,但概念是相同的。

于 2011-07-25T12:01:10.697 回答
1

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

var isDeleting = false; 

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

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

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

于 2011-07-25T11:58:12.593 回答
0

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

于 2011-12-29T04:19:15.897 回答
0

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

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

禁用

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

重新启用

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

另外,我不知道为什么,除非我包含“jquery-ui.js”,否则设置回TRUE不起作用。

希望这可以帮助

于 2013-11-01T17:57:22.030 回答