HTML drag & drop best practices

I am compiling a fairly simple HTML / Javascript / PHP form (I'm pretty new to all of these cases). One form field requires users to select an option from a fairly long list (using the drop-down list). The content of the list is unlikely to ever change. Is there a “better” way to populate a list than just using tags <option>:

<select name="longlist">
    <option value="one">One</option>
    <option value="two">Two</option>
    ....
    <option value="sixty">Sixty</option>
</select>
+3
source share
3 answers

As a result, HTML will always have parameter tags, but you can generate them on the fly using PHP or jQuery.

for instance

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");
});
+6
source

, , .

, , : , , - . jQuery <option> , , .

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();

HTML:

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

If you do not need to have a number in the form of a word, for example. 1 vs. 'one', you can save yourself the difficulty of creating a large array of words for the loop.

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

Source: https://habr.com/ru/post/1758648/


All Articles