문제

Given the following code:

    if ($selector.html().indexOf("radio") > 0) {

        if ($oldSelected != null) {
            // enable clicks on old selected radio button
            $oldSelected.children().bind('click', function () { return true; });
        }

        // disable click on selected 
        $selector.children().bind('click', function () { return false; });
        $oldSelected = $selector;
    }

The disabling code works fine. Once executed the element is no longer clickable. However, when trying to re-enable the click event, nothing happens.

Suggestions?

도움이 되었습니까?

해결책

You're just binding click handlers over and over again. You need to unbind() the ones you don't want active.

Try this:

$oldSelected.children().unbind('click').bind('click', function () { return true; });

On second thought, unbind('click') may be all you need if there's no other logic in your click handlers.

다른 팁

Try something like:

var handler = function () {
    // do some stuff
};

// First
el.bind('click', handler);

// Later on...
el.unbind('click', handler);

Remember to save the callback so that later on you can unbind. Unbind docs.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top