如果列表仅包含单个元素,我如何更改 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>
你可以通过更改 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}"