如何使用javascript更改选项标签上的“selected”属性

时间:2017-06-25 19:51:31

标签: javascript html

我正在为在线订单创建一个网络表单。我想使用javaScript将选择框默认为当前月份,但它似乎对我不起作用,我不知道为什么。我是javascript的新手,但我想我想出来了。我错过了什么或误解了什么吗? 这是HTML:

<label for="orderMonth">Month</label>
            <select name="month" id="orderMonth" onchange="setselect()">
                <option value="01" id="m0">January</option>
                <option value="02" id="m1">February</option>
                <option value="03" id="m2">March</option>
                <option value="04" id="m3">Aprl</option>
                <option value="05" id="m4">May</option>
                <option value="06" id="m5">June</option>
                <option value="07" id="m6">July</option>
                <option value="08" id="m7">August</option>
                <option value="09" id="m8">September</option>
                <option value="10" id="m9">October</option>
                <option value="11" id="m10">November</option>
                <option value="12" id="m11">December</option>
            </select>

以下是我尝试它的第一种方式:

<script>
        var month = getMonth();
        var opt = document.getElementById("m"+month);
        opt.setAttribute("selected");
    </script>

和我试过的第二种方式:

<script>
        var month = getMonth();
        if (month == 0) {
            document.getElementById("m0").selected = true;}
        else if (month == 1) {
            document.getElementById("m1").selected = true;}
        else if (month == 2) {
            document.getElementById("m2").selected = true;}
        else if (month == 3) {
            document.getElementById("m3").selected = true;}
        else if (month == 4) {
            document.getElementById("m4").selected = true;}
        else if (month == 5) {
            document.getElementById("m5").selected = true;}
        else if (month == 6) {
            document.getElementById("m6").selected = true;}
        else if (month == 7) {
            document.getElementById("m7").selected = true;}
        else if (month == 8) {
            document.getElementById("m8").selected = true;}
        else if (month == 9) {
            document.getElementById("m9").selected = true;}
        else if (month == 10) {
            document.getElementById("m10").selected = true;}
        else if(month == 11) {
            document.getElementById("m11").selected = true;}
    </script>

有人想到吗?

2 个答案:

答案 0 :(得分:1)

您必须创建一个新的Date obj来占用月份。查看下面的代码:

&#13;
&#13;
var today=new Date();
var month = today.getMonth();
        if (month == 0) {
            document.getElementById("m0").selected = true;}
        else if (month == 1) {
            document.getElementById("m1").selected = true;}
        else if (month == 2) {
            document.getElementById("m2").selected = true;}
        else if (month == 3) {
            document.getElementById("m3").selected = true;}
        else if (month == 4) {
            document.getElementById("m4").selected = true;}
        else if (month == 5) {
            document.getElementById("m5").selected = true;}
        else if (month == 6) {
            document.getElementById("m6").selected = true;}
        else if (month == 7) {
            document.getElementById("m7").selected = true;}
        else if (month == 8) {
            document.getElementById("m8").selected = true;}
        else if (month == 9) {
            document.getElementById("m9").selected = true;}
        else if (month == 10) {
            document.getElementById("m10").selected = true;}
        else if(month == 11) {
            document.getElementById("m11").selected = true;}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您更改了代码,则代码可以正常运行:

var month = getMonth();

var month = new Date().getMonth();

但是,由于月份编号与选项索引匹配,您只需将选择的 selectedIndex 设置为月份编号:

window.onload = function() {
  document.getElementById('orderMonth').selectedIndex = new Date().getMonth();
}
<label for="orderMonth">Month</label>
<select name="month" id="orderMonth" onchange="setselect()">
  <option value="01" id="m0">January</option>
  <option value="02" id="m1">February</option>
  <option value="03" id="m2">March</option>
  <option value="04" id="m3">Aprl</option>
  <option value="05" id="m4">May</option>
  <option value="06" id="m5">June</option>
  <option value="07" id="m6">July</option>
  <option value="08" id="m7">August</option>
  <option value="09" id="m8">September</option>
  <option value="10" id="m9">October</option>
  <option value="11" id="m10">November</option>
  <option value="12" id="m11">December</option>
</select>

你也可以这样做:

document.getElementById('orderMonth').options[new Date().getMonth()].selected = true;