jquery产品可用性结果不正确

时间:2012-12-21 04:42:21

标签: jquery

我制作了一个自定义产品可用性代码,用于检查产品尺寸(T恤)是否可用。问题是,即使尺寸不可用,它显示defualt选择尺寸其价格+另一个尺寸的价格,当我们点击不可用的尺寸时可用。例如。三种尺寸,中,小和大。假设中等大小可用,那么当我们点击小时,它不应该显示任何价格,而是显示其中所有可用的价格。如果有更多的尺寸,如超小,它显示其价格的其余尺寸价格,它不应该。虽然当所有尺寸都可用时它工作正常。我写的代码是否有人可以建议我在哪里错了?

    $.fn.contentTabs = function(o){
        // default options
        var options = $.extend({
            activeClass:'active',
            addToParent:false,
            autoHeight:false,
            autoRotate:false,
            animSpeed:400,
            switchTime:3000,
            effect: 'none', // "fade", "slide"
            tabLinks:'a',
            event:'click'
        },o);

        return this.each(function(){
            var tabset = $(this);
            var tabLinks = tabset.find(options.tabLinks);
            var tabLinksParents = tabLinks.parent();
            var prevActiveLink = tabLinks.eq(0), currentTab, animating;
            var tabHolder;

            // init tabLinks
            tabLinks.each(function(){
                var link = $(this);
                var href = link.attr('href');
                var parent = link.parent();
                href = href.substr(href.lastIndexOf('#'));

                // get elements
                var tab = $(href);
                link.data('cparent', parent);
                link.data('ctab', tab);

// find tab holder
if(!tabHolder && tab.length > 0) {
   tabHolder = tab.parent();

}

                // show only active tab
                if((options.addToParent ? parent : link).hasClass(options.activeClass)) {
                    prevActiveLink = link; currentTab = tab;
                    tab.removeClass(tabHiddenClass).width("");
                    contentTabsEffect[options.effect].show({tab:tab, fast:true});
                } else {
                    contentTabsEffect[options.effect].hide({tab:tab, fast:true});
                    tab.width(tab.width()).addClass(tabHiddenClass);
                }

                // event handler
                link.bind(options.event, function(e){
                    if(link != prevActiveLink && !animating) {
                        switchTab(prevActiveLink, link);
                        prevActiveLink = link;
                    }
                    e.preventDefault();
                });
                if(options.event !== 'click') {
                    link.bind('click', function(e){
                        e.preventDefault();
                    });
                }
            });

            // tab switch function
            function switchTab(oldLink, newLink) {
                animating = true;
                var oldTab = oldLink.data('ctab');
                var newTab = newLink.data('ctab');
                currentTab = newTab;

                // refresh pagination links
                (options.addToParent ? tabLinksParents : tabLinks).removeClass(options.activeClass);
                (options.addToParent ? newLink.data('cparent') : newLink).addClass(options.activeClass);

                // hide old tab
                resizeHolder(oldTab, true);
                contentTabsEffect[options.effect].hide({
                    speed: options.animSpeed,
                    tab:oldTab,
                    complete: function() {
                        // show current tab
                        resizeHolder(newTab.removeClass(tabHiddenClass).width(''));
                        contentTabsEffect[options.effect].show({
                            speed: options.animSpeed,
                            tab:newTab,
                            complete: function() {
                                oldTab.width(oldTab.width()).addClass(tabHiddenClass);
                                animating = false;
                                resizeHolder(newTab, false);
                                autoRotate();
                            }
                        });
                    }
                });
            }

            // holder auto height
            function resizeHolder(block, state) {
                var curBlock = block && block.length ? block : currentTab;
                if(options.autoHeight && curBlock) {
                    tabHolder.stop();
                    if(state === false) {
                        tabHolder.css({height:''});
                    } else {
                        var origStyles = curBlock.attr('style');
                        curBlock.show().css({width:curBlock.width()});
                        var tabHeight = curBlock.outerHeight(true);
                        if(!origStyles) curBlock.removeAttr('style'); else curBlock.attr('style', origStyles);
                        if(state === true) {
                            tabHolder.css({height: tabHeight});
                        } else {
                            tabHolder.animate({height: tabHeight}, {duration: options.animSpeed});
                        }
                    }
                }
            }
            if(options.autoHeight) {
                $(window).bind('resize orientationchange', function(){
                    resizeHolder(currentTab, false);
                });
            }

            // autorotation handling
            var rotationTimer;
            function nextTab() {
                var activeItem = (options.addToParent ? tabLinksParents : tabLinks).filter('.' + options.activeClass);
                var activeIndex = (options.addToParent ? tabLinksParents : tabLinks).index(activeItem);
                var newLink = tabLinks.eq(activeIndex < tabLinks.length - 1 ? activeIndex + 1 : 0);
                prevActiveLink = tabLinks.eq(activeIndex);
                switchTab(prevActiveLink, newLink);
            }
            function autoRotate() {
                if(options.autoRotate && tabLinks.length > 1) {
                    clearTimeout(rotationTimer);
                    rotationTimer = setTimeout(nextTab, options.switchTime);
                }
            }
            autoRotate();
        });
    }

0 个答案:

没有答案