2012-12-18 65 views
1

我正在jquery下拉列表。通常在我的下拉菜单中有多个选项可供选择,但对于某些产品,只有一个选项,我希望脚本能够选择值(如果只有一个选项),并禁用下拉菜单。现有的代码是:选择如果只有一个选项可用下拉&禁用

RonisCatalog.selectSwatches = function(element, options){ 
if (jQuery('body .swatches-container').size()==0) 
    return ; 

else  
var selectOptions = jQuery(element).find('option'), 
    selectOptionsValues = {}; 
var selectLabel = ''; 
jQuery.each(selectOptions,function(idx,selectOption){ 
    if (jQuery(selectOption).val()=='') 
     selectLabel = jQuery(selectOption).html(); 
    selectOptionsValues[jQuery(selectOption).val()] = jQuery(selectOption).val(); 
}); 
jQuery('body .swatches-container .swatch-type-id-'+element.attributeId).parents('.swatch-list-continer').children('.swatch-list-label').html('<label id="attribute'+element.attributeId+'_label" >Select '+selectLabel+'</label>'); 
jQuery('body .swatches-container .swatch-type-id-'+element.attributeId+' .swatch-option').addClass('swatch-disabled').removeClass('swatch-active'); 

jQuery.each(options,function(idx,option){ 
    if (typeof selectOptionsValues[option.id] != 'undefined') 
     jQuery('body .swatches-container .swatch-type-id-'+element.attributeId+' .swatch-option-id-'+option.id).removeClass('swatch-disabled'); 
}); 
}; 

我想在之间使用此选项:

else if (jQuery('body .swatches-container').size()==1) { 


} 

和功能如上所述。

任何帮助,将不胜感激。

感谢

回答

0

你可以试试这个:

else if ($jQuery('body .swatches-container').size() == 1) { 
    $jQuery('body .swatches-container').val('0').attr("disabled", "disabled"); 
} 
+0

感谢贾斯汀,我加了这段代码。它不会选择第一个选项,但会禁用样本容器值,因此您无法选择给定值。 – Avian

相关问题