简体   繁体   中英

Yii2 Dropdown list without value attribute inside option element

I'm creating a year dropdown list using the following code:

<?php
$range = range(2016, 2026);
echo $form->field($model, 'year')->dropDownList($range)->label("Year");
?>

It outputs:

<select id="testform-year" class="form-control" name="TestForm[year]">
    <option value="0">2016</option>
    <option value="1">2017</option>
    <option value="2">2018</option>
    <option value="3">2019</option>
    <option value="4">2020</option>
    <option value="5">2021</option>
</select>

But I want the output without value attribute inside option, like:

<select id="testform-year" class="form-control" name="TestForm[year]">
    <option>2016</option>
    <option>2017</option>
    <option>2018</option>
    <option>2019</option>
    <option>2020</option>
    <option>2021</option>
</select>

Try this: This will create the dropdown wuth value as 2016 and not 0.

<?php
$range = range(2016, 2026);
echo $form->field($model, 'year')->dropDownList(array_combine($range, $range))->label("Year");
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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