php - HTML Drop Down List Best Practices - Stack Overflow

I'm putting together a fairly simple HTMLJavascriptPHP form (I am fairly new to all of these). O

I'm putting together a fairly simple HTML/Javascript/PHP form (I am fairly new to all of these). 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:

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

I'm putting together a fairly simple HTML/Javascript/PHP form (I am fairly new to all of these). 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:

<select name="longlist">
    <option value="one">One</option>
    <option value="two">Two</option>
    ....
    <option value="sixty">Sixty</option>
</select>
Share asked Aug 8, 2010 at 21:49 user130076user130076
Add a ment  | 

3 Answers 3

Reset to default 6

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.

For example

PHP

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

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.

E.g

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:

<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, e.g. 1 vs. 'one', you can save yourself the trouble of having to create the large array of words for the loop.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744808672a4594932.html

相关推荐

  • php - HTML Drop Down List Best Practices - Stack Overflow

    I'm putting together a fairly simple HTMLJavascriptPHP form (I am fairly new to all of these). O

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信