简体   繁体   English

将两个点击事件添加到同一按钮仅一次

[英]Adding two click events to the same button only works once

Basically I'm trying to make a button be able to handle editing of an element. 基本上,我试图使按钮能够处理元素的编辑。 I want it so that when I click on the Edit button, it changes the text to Save Changes and adds a class which will then bind the button to another click event so that when they click Save Changes , it'll alert Saved and change the text back to Edit . 我想要它,以便当我单击“ Edit按钮时,它将文本Save Changes为“ Save Changes并添加一个类,该类随后将按钮绑定到另一个单击事件,以便当他们单击“ Save Changes ,它将警告Saved并更改文字回到Edit It does this perfectly once. 它会完美地执行一次。 If you continue to try to do it, it simply won't add the class or change the text anymore. 如果您继续尝试这样做,它将不再添加类或更改文本。

Here is a demo on jsfiddle 这是jsfiddle上的演示

The code: 编码:

$(function() {
$button = $('button[name="edit"]');
$button.on('click', $button, function() {
    var $that = $(this);
    $that.text('Save Changes');
    $that.addClass('js-editing');
    if ($that.hasClass('js-editing')) {
        $that.off('click').on('click', $that, function() {
            alert('Saved!');
            $that.text('Edit');
            $that.removeClass('js-editing');
        });
    }
});

});​ });

Try this http://jsfiddle.net/bpD8B/4/ 试试这个http://jsfiddle.net/bpD8B/4/

$(function() {
    $button = $('button[name="edit"]');
    $button.on('click', $button, function() {
        var $that = $(this);
        if($that.text()=='Edit'){
          $that.text('Save Changes');
          $that.addClass('js-editing');
        }
        else{
                alert('Saved!');
                $that.text('Edit');
                $that.removeClass('js-editing');
        }
    });
});

You never add back the original handler after calling off(), which removes it. 您永远不会在调用off()后再添加原始处理程序,从而将其删除。

That being said, it might be easier to have two buttons, with appropriate click handlers, and then use hide() and show() to alternate which one is available. 话虽如此,拥有两个带有适当单击处理程序的按钮,然后使用hide()和show()来交替选择哪个按钮可能更容易。 To the end user it should look and act exactly the same, and to you it will be a lot less of a headache to code. 对于最终用户而言,它的外观和行为应该完全相同,并且对您而言,编写代码的麻烦将大大减轻。

Example: http://jsfiddle.net/VgsLA/ 示例: http//jsfiddle.net/VgsLA/

I think in the end, this code is more robust and manageable. 我认为最终,此代码更加健壮和易于管理。

This is just a logic problem. 这只是一个逻辑问题。 And with $that.off('click').on('click', $that, function() { you are delegating to itself, which is not how you should do it. 并通过$that.off('click').on('click', $that, function() {委托给自己,这不是您应该做的。

Here is a solution using your code: 这是使用您的代码的解决方案:

$(function() {

    $button = $('button[name="edit"]');
    $button.on('click', $button, function() {
        var $that = $(this);
        if ($that.hasClass('js-editing')) {
            alert('Saved!');
            $that.text('Edit');
            $that.removeClass('js-editing');        
        } else {      
            $that.text('Save Changes'); 
            $that.addClass('js-editing');
        }
    });

});​

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM