简体   繁体   English

HTML下拉列表最佳实践

[英]HTML Drop Down List Best Practices

I'm putting together a fairly simple HTML/Javascript/PHP form (I am fairly new to all of these). 我正在整理一个相当简单的HTML / Javascript / PHP表单(我对所有这些都相当新)。 One field of the form requires users to select an option from a reasonably long list (using a drop down list). 表单的一个字段要求用户从合理长的列表中选择一个选项(使用下拉列表)。 The contents of the list are extremely unlikely to ever change. 列表的内容极不可能改变。 Is there a "better" way to populate the list than simply using a lot of <option> tags: 填充列表是否有“更好”的方式,而不仅仅是使用大量的<option>标记:

<select name="longlist">
    <option value="one">One</option>
    <option value="two">Two</option>
    ....
    <option value="sixty">Sixty</option>
</select>

The resulting HTML will always have to have the option tags, but you may be able to generate it on the fly using PHP or jQuery. 生成的HTML总是必须有选项标签,但您可以使用PHP或jQuery动态生成它。

For example 例如

PHP PHP

<select>
<?php
$myArray=array("One","Two","Three", "Four", "Five");
while (list($key, $val) = each($myArray))
  {
  echo "<option>" . $val . "</option>"
  }
?> 
</select>

jQuery jQuery的

<select id="array-test"></select>

var myArray= ["One","Two","Three","Four","Five"];

$.each(myArray, function(index, value) { 
  $("#array-test").append("<option>" + value + "</option");
});

From a usability point of view, if the number of options is really that long, it's difficult to find the option you actually want. 从可用性的角度来看,如果选项的数量确实很长,则很难找到您真正想要的选项。

Try to find a way to split the options up into categories and perhaps show two dropdowns: the first to choose the category, and the second to show only the options within the category. 尝试找到一种方法将选项拆分为类别,并可能显示两个下拉菜单:第一个选择类别,第二个只显示类别中的选项。 You can use jQuery to dynamically create the <option> s for the second dropdown based on the choice made in the first. 您可以使用jQuery根据第一个<option>为第二个下拉列表动态创建<option>

Eg 例如

options = {
   "one": [1, 2, 3, 4],
   "two": [11, 12, 13, 14],
   "three": [21, 22, 23, 24]
}

$("select.select1").change(function() {
   var category = $(this).val() || $(this).text();
   if (options[category]) {
      $("select.select2").empty();
      for (var i in options[category]) {
         var newOption = $("<option>").text(options[category][i]);
         $("select.select2").append(newOption);
      }
   } else {
      $("select.select2").html("<option>Select a category first</option>");
   }
});

$("select.select1").change();

With HTML: 使用HTML:

<select class="select1">
   <option>one</option>
   <option>two</option>
   <option>three</option>
</select>
<select class="select2">
   <!-- I am dynamically generated -->
</select>

If you don't need to have the number in word form, eg 1 vs. 'one' , you can save yourself the trouble of having to create the large array of words for the loop. 如果您不需要使用单词形式的数字,例如1 vs. 'one' ,您可以省去必须为循环创建大量单词的麻烦。

<select>
<?php
    for ($i = 1; $i <= 60; $i++){
        echo "<option>" . $i . "</option>";
    }
?>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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