prettyCheckboxes使父div可点击?

时间:2011-03-28 16:37:39

标签: jquery

我正在使用prettyCheckboxes,我想更改它,以便也可以单击div包装器以及单选按钮。单选按钮/标签的当前JS是

        $label = $('label[for="'+$(this).attr('id')+'"]');

        // Add the checkbox holder to the label
        $label.prepend("<span class='holderWrap'><span class='holder'></span></span>");

        // If the checkbox is checked, display it as checked
        if($(this).is(':checked')) { $label.addClass('checked'); };

        // Assign the class on the label
        $label.addClass(settings.className).addClass($(this).attr('type')).addClass(settings.display);

        // Assign the dimensions to the checkbox display
        $label.find('span.holderWrap').width(settings.checkboxWidth).height(settings.checkboxHeight);
        $label.find('span.holder').width(settings.checkboxWidth);

        // Hide the checkbox
        $(this).addClass('hiddenCheckbox');

        // Associate the click event
        $label.bind('click',function(){
            $('input#' + $(this).attr('for')).triggerHandler('click');

            if($('input#' + $(this).attr('for')).is(':checkbox')){
                $(this).toggleClass('checked');
                $('input#' + $(this).attr('for')).checked = true;

                $(this).find('span.holder').css('top',0);
            }else{
                $toCheck = $('input#' + $(this).attr('for'));

                // Uncheck all radio
                $('input[name="'+$toCheck.attr('name')+'"]').each(function(){
                    $('label[for="' + $(this).attr('id')+'"]').removeClass('checked');  
                });

                $(this).addClass('checked');
                $toCheck.checked = true;
            };
        });

我想这样做,因此$(this).parent()也会调用click函数。

我有什么想法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

使用.add创建一个自我+父的jQuery对象:

var labelAndParent = $label.add($(this).parent());

然后绑定到该对象:

labelAndParent.bind('click',function(){
    // ...
});
相关问题