0

我有一个表格,我希望人们填写他们的出生年份。您只能在 18 岁以上的情况下申请,所以在出生年份字段中,我希望最小年龄为 18 岁。我已按如下所示进行了此操作,但是我想将最近一年的数字排在最前面,最老的一年排在底部。目前我先显示 1913,但我想先显示 1994。这是我的代码

<div id="my-container"></div>

<script>
      var d = new Date();
      var y = d.getFullYear();
      var selectList = "<select>";
      for (var x = (y-100); x < (y - 18); x++) {
          selectList += "<option>" + x + "</option>";
      }
      selectList += "</select>";
      $('#my-container').html(selectList);
      </script>
4

3 回答 3

2

正如尼罗所说

var d = new Date();
var y = d.getFullYear();
var selectList = "<select>";
for (var x = y-18; x >= y-100; x--) {
    selectList += "<option>" + x + "</option>";
}
selectList += "</select>";
$('#my-container').html(selectList);

http://jsfiddle.net/samliew/WzwKw/

于 2013-02-28T03:22:33.247 回答
1

试试这个

 <script>
  var d = new Date();
  var y = d.getFullYear();
  var selectList = "<select>";
  for (var x = y-18; x > y - 100; x--) {
      selectList += "<option>" + x + "</option>";
  }
  selectList += "</select>";
  $('#my-container').html(selectList);
  </script>
于 2013-02-28T03:22:08.180 回答
0
<script>
    $( document ).ready( function() {
    var d = new Date();
    var y = d.getFullYear();
    var selectList = "<select>";
    for (var x = (y-18); x >= (y - 100); x--) {
      selectList += "<option>" + x + "</option>";
    }
    selectList += "</select>";
    $('#my-container').html(selectList);
    });
</script>

请试试这个。

于 2013-02-28T04:54:22.090 回答