【问题标题】:HTML <select> with element list带有元素列表的 HTML <select>
【发布时间】:2018-06-19 11:27:01
【问题描述】:

以下表单完全符合预期,并提供了一个下拉列表供您选择:

<select id="f-heroes" class="c-form-select__dropdown">
      <option value="" disabled selected>Heroes</option>
      <option value="captainAmerica">Captain America</option>
      <option value="ironMan">Iron Man</option>
      <option value="blackWidow">Black Widow</option>
      <option value="thor">Thor</option>
</select>

我想要做的是:

<select id="f-heroes" class="c-form-select__dropdown">
      <option value=""><?echo getElement('hero');?></option>         
</select>

这会获取“英雄”列表,但会在选项窗口下方和外部显示,并且无法选择值。

我可能在这里遗漏了一些非常简单的东西,但似乎无法使其正常工作,非常感谢任何帮助。

【问题讨论】:

  • 你能定义getElement()函数吗?
  • 如果 getElement(...) 返回一个列表,您不能只在单个

标签: html forms variables select element


【解决方案1】:

看起来你在这里混合了 PHP 和 JavaScript。根据我对您的问题的理解,您想要创建一个循环,不断为您的每个超级英雄创建 's。

如果正确,您可以使用 PHP 并执行以下操作:

<select id="f-heroes" class="c-form-select__dropdown">
<option value="" disabled selected>Heroes</option>
<?php
$superHeroes = array('Captain America','Iron Man','Black Widow','Thor');
$i = 0;
while ($i < count($superHeroes)) {
   $name = $superHeroes[$i];
   echo "<option value='".$name."'>".$name."</option>";
   $i++;
}?>
</select>

【讨论】:

  • 感谢@Pete Boucher,这可以满足我的需要,只是与同事设置其他页面的方式略有不同。不过绝对可以用这个,再次感谢。
猜你喜欢
  • 2019-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多