سؤال

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