2012-12-18 87 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

相關問題