将指定的索引放在选择框顶部的数组中

时间:2016-01-22 08:36:59

标签: php handlebars.js handlebarshelper

我有以下数组:

Array
(
   [2] => Afganistan
   [3] => Africa
   [4] => Albania
   [5] => Alegira
   [6] => Turcia
 )

在模板.handlebars中我做:

{{#each oForm.lCountry}}
    <option value="{{@key}}">
      {{this}}
    </option>
{{/each}}

在selectbox的第一个值中,我得到Afganistan,如何对第一个值Turcia进行验证? 请帮我。 Thx提前和抱歉我的英语

1 个答案:

答案 0 :(得分:1)

我建议只标记&#34; Turcia&#34;如选中。

Handlebars Helper:(Javascript版本)

Handlebars.registerHelper('eq', function(a, b, block) {
    return a == b ? block.fn(this) : block.inverse(this);
});

<强>模板:

{{#each oForm.lCountry}}
    <option value="{{@key}}" {{#eq this 'Turcia'}}selected="true"{{/eq}}>
      {{this}}
    </option>
{{/each}}

<强>输出:

<option value="2" >
    Afganistan
</option>
...
<option value="6" selected="true">
    Turcia
</option>

现在&#34; Turcia&#34;,默认会被选中。这更符合逻辑,因此您可以保留字母顺序。

如果你想要&#34; Turcia&#34;无论如何首先出现,只需在PHP中更改数组的顺序。