0

如果列表仅包含单个元素,我如何更改 SELECT 元素的样式?

我有:

<select class="attribute_select">
<option selected="selected">001</option>
<option>002</option>
</select>

我需要:

<select class="attribute_select_single">
<option selected="selected">001</option>
</select>

更新

<select name="{$groupName}" id="group_{$id_attribute_group|intval}" class="attribute_select" onchange="findCombination();getProductAttribute();">
{foreach from=$group.attributes key=id_attribute item=group_attribute}
<option value="{$id_attribute|intval}"{if (isset($smarty.get.$groupName) && $smarty.get.$groupName|intval == $id_attribute) || $group.default == $id_attribute} selected="selected"{/if} title="{$group_attribute|escape:'htmlall':'UTF-8'}">
{$group_attribute|escape:'htmlall':'UTF-8'}</option>
{/foreach}
</select>
4

1 回答 1

0

您可以通过更改 SELECT 元素的类来使用 jQuery 来做到这一点:

$(function() {    
  if ($('select.attribute_select option').length == 1)
    $('select.attribute_select').attr('class', 'attribute_select_single');
}

您也可以使用 @count 修饰符在模板中执行此操作:

代替:

class="attribute_select"

和:

class="{if $group.attributes|@count eq 1}attribute_select_single{else}attribute_select{/if}"
于 2013-10-12T17:06:28.243 回答