简体   繁体   English

选择下拉菜单中是否只有一个选项可用并禁用

[英]select if only one option available in dropdown & disable

I'm working on a jquery dropdown. 我正在处理一个jQuery下拉列表。 Usually there are multiple options in my drop down to select from but for some products there is only one option I want the script to be able to select the value if only one option is there and disable the dropdown. 通常,我的下拉菜单中有多个选项可供选择,但是对于某些产品,只有一个选项是我希望脚本能够在只有一个选项的情况下选择该值并禁用该下拉菜单。 Existing code is: 现有代码为:

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');
});
};

I want to use this option in between: 我想在以下之间使用此选项:

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


}

and functionality as described above. 和上述功能。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks 谢谢

You could try this: 您可以尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM