1

我有一个包含数字 1-20 的下拉菜单的 HTML 页面 (index.php)。有没有一种更简单的方法可以将选项添加到下拉菜单中,而不仅仅是手动添加每个选项,例如:

<option value=1>1</option>
...
<option value=20>20</option>
4

2 回答 2

4

As you already mention php in your tags, then using a for-loop when generating the web page would be possible:

for ($i = 1; $i<=20; $i++) {
    echo '<option value="'.$i.'">'.$i.'</option>';
}

You should further think about spending some more quotes to the value attribute as in my code above.

于 2012-08-12T21:10:37.613 回答
0

Easiest would be a for loop :

<?php for ($i = 1; $i <=20; $i++) : ?>
    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
于 2012-08-12T21:12:52.527 回答