如何用特定值填充下拉列表顶部

时间:2017-11-22 16:04:05

标签: javascript jquery

如何使用JavaScript中的特定值填充下拉列表我希望最高值 7月

此时第一个值显示一月我希望七月位于最前面  七月

之后的值
<select id="monthToSelect"></select>

var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var option = '';

for (i = 0; i < months.length; i++) {
  option += '<option value="' + months[i] + '">' + months[i] + '</option>';
}

$('#monthToSelect').append(option);

4 个答案:

答案 0 :(得分:1)

我认为你需要array.sort()

<强>样本

&#13;
&#13;
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

var sorted =months.sort();
var option = '';

for (i=0;i<months.length;i++){
   option += '<option value="'+ sorted [i] + '">' + sorted [i] + '</option>';
}

$('#monthToSelect').append(option);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=monthToSelect > </div>
&#13;
&#13;
&#13;

修改

如果您只需要特定月份作为第一个值,那么

&#13;
&#13;
  var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
       
   var option = '';
         option += '<option value="'+ months [3] + '">' + months [3] + '</option>';
        for (i=0;i<months.length;i++){
           option += '<option value="'+ months [i] + '">' + months [i] + '</option>';
        }

        $('#monthToSelect').append(option);

  
&#13;
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <div id=monthToSelect > </div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

未经测试...

$("#monthToSelect option").click(function() {
    var clickedOption = $(this);
    clickedOption.remove();
    clickedOption.insertBefore($("#monthToSelect").find('option:eq(1)'));
});

答案 2 :(得分:0)

我对@Sajeetharan的代码进行了一些修改,实际上首先显示了7月份并删除了组合框的重复值。

&#13;
&#13;
  var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
       
   var option = '';
         option += '<option value="'+ months [6] + '">' + months [6] + '</option>';
        for (i=0;i<months.length;i++){
           if(option.indexOf(months[i])==-1)
               option += '<option value="'+ months [i] + '">' + months [i] + '</option>';
        }

        $('#monthToSelect').append(option);

  
&#13;
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   <div id=monthToSelect > </div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

我希望以下代码可以帮助您获得预期的输出

$(function(){

var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var option = '';
var startMonth = 6;  //choose which month you need first via month array index
var mts = months.slice(startMonth).concat(months.slice(0,startMonth));
for (var i=0;i<mts.length;i++){
   option += '<option value="'+ mts [i] + '">' + mts [i] + '</option>';
}
$('#monthToSelect').append(option);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="monthToSelect"></select>