Easy way to add drop down menu with 1 - 100 without doing 100 different options?

前端 未结 7 1528

I was just wondering if there was a simple shortcut to add options to a dropdown menu for the numbers 1 to 100 instead of having to do the following:

相关标签:
7条回答
  • 2021-02-01 05:28

    Jquery One-liners:

    ES6 + jQuery:

    $('#select').append([...Array(100).keys()].map((i,j) => `< option >${i}</option >`))
    

    Lodash + jQuery:

    $('#select').append(_.range(100).map(function(i,j){ return $('<option>',{text:i})}))
    
    0 讨论(0)
  • 2021-02-01 05:32

    In Html5, you can now use

    <form>
    <input type="number" min="1" max="100">
    </form>
    
    0 讨论(0)
  • 2021-02-01 05:33

    Not with pure HTML as far as I know.

    But with JS or PHP or another scripting language such as JSP, you can do it very easily with a for loop.

    Example in PHP:

    <select>
    <?php
        for ($i=1; $i<=100; $i++)
        {
            ?>
                <option value="<?php echo $i;?>"><?php echo $i;?></option>
            <?php
        }
    ?>
    </select>
    
    0 讨论(0)
  • 2021-02-01 05:36

    As everyone else has said, there isn't one in html; however, you could use PUG/Jade. In fact I do this often.

    It would look something like this:

    select
      - var i = 1
      while i <= 100
        option=i++
    

    This would produce:

    0 讨论(0)
  • 2021-02-01 05:45

    I see this is old but... I dont know if you are looking for code to generate the numbers/options every time its loaded or not. But I use an excel or open office calc page and place use the auto numbering all the time. It may look like this...

    | <option> | 1 | </option> |

    Then I highlight the cells in the row and drag them down until there are 100 or the number that I need. I now have code snippets that I just refer back to.

    0 讨论(0)
  • 2021-02-01 05:47

    Are you using JavaScript or jQuery besides the html? If you are, you can do something like:

    HTML:

    <select id='some_selector'></select>​
    

    jQuery:

    var select = '';
    for (i=1;i<=100;i++){
        select += '<option val=' + i + '>' + i + '</option>';
    }
    $('#some_selector').html(select);
    

    As you can see here.

    Another option for compatible browsers instead of select, you can use is HTML5's input type=number:

    <input type="number" min="1" max="100" value="1">
    
    0 讨论(0)
提交回复
热议问题