开发者

jQuery unbinding click event on a radio button that is already selected

开发者 https://www.devze.com 2023-03-20 06:26 出处:网络
I am trying to create some parametric filters but i do not want to invoke the function that makes my search AJAX call when a radio button filter is already selected.

I am trying to create some parametric filters but i do not want to invoke the function that makes my search AJAX call when a radio button filter is already selected.

$('#contentType input:enabled').bind('click', function(e){
    var $this = $(this),
        $type = this.type,
        $id = this.id;
    if ($type === 'radio') {
        if ($id === 开发者_运维知识库'allContent'){
            sFrm.$boxes.attr({'checked': false});
            sFrm.$specificRadio.attr({'disabled': true});   
            searchAjax();
            removeFilter();
        }
    } else if ($type === 'checkbox') {
        if (sFrm.$boxes.filter(':checked').length === 0) {
            sFrm.$allRadio.trigger('click');        
        } else {
            var filterConfig = {
                index: $('.subFilter').index($this),
                txt: jQuery.trim($this.parent().text())
            }                   
            sFrm.$allRadio.attr({'checked': false});        
            sFrm.$specificRadio.attr({'disabled': false, 'checked': true}); 
            searchAjax();
            if ($this.is(':checked')) {
                addFilter(filterConfig);
            } else {
                removeFilter(filterConfig.index);
            }               
        }           
    }   
});

So the jQuery above looks at ALL enabled inputs in the collection and when a user clicks the allContent radio button the searchAjax function is invoked.

This is correct when said radio button is not already selected/checked. At the moment the function is still invoked when the button is selected and i would like to stop this behaviour but cannot figure out how?

EDIT

Not sure i explained correctly. Hopefully this jsFiddle will help:

http://jsfiddle.net/QsbRp/3/

Basically if All content types is checked already and is clicked when checked then searchAjax should not be invoked. It currently invokes the function.

When it is not checked and clicked then it should behave as it is now (disable sibling radio button and uncheck all associated checkboxes).


Add at the top of your handler:

if ($this.is(":checked")) {
    return;
}

The event handler will return and not execute the rest of the function if the radio button is currently selected.

0

精彩评论

暂无评论...
验证码 换一张
取 消