Drupal 中的产品页面:如果只有一个选项 ("size") 可用,则需要在 select 选项卡上调整 css

Product page in Drupal: Need to adjust css on the select tab if there is only one option ("size") available

在 Drupal Commerce 的产品页面上,一些项目有尺寸选项,而另一些则没有。我需要为那些只有一种尺寸选项的 select 标签调整 css。这是我的 jquery 尝试无济于事:

if($(".page-store select.form-select option:selected").index()==0) {
  $(page-store select.form-select).addClass("one-option-only");
};

我怎样才能做到这一点?

你的逻辑有点不对,语法错误。

//iterate each select
$('.page-store select.form-select').each(function() {
    //see if the current select has exactly one option
    if ($(this).find('option').length == 1) {
        //add the class
        $(this).addClass('one-option-only');
    }
});

检查.children("option").length

$(document).ready(function() {
  $(".page-store select.form-select").each(function() {
    if ($(this).children("option").length === 1) {
      $(this).addClass("one-option-only");
    }
  });
});