简体   繁体   English

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

[英]How do I change the “selected” attribute on an option tag with javascript

I'm creating a web form for an online order. 我正在为在线订单创建Web表单。 I want to make the select box default to the current month using javaScript but it doesn't seem to be working for me and I'm not sure why. 我想使用javaScript将选择框默认设置为当月,但是它似乎不适用于我,我不确定为什么。 I am new to javascript but I thought I figured this out. 我是javascript新手,但我想我已经弄清楚了。 Am I missing something or mis-interpreting something? 我错过了什么还是误解了什么? Here is the HTML: 这是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>

And here is the script the first way I tried it: 这是我尝试的第一种脚本:

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

and the second way I tried it: 第二种方法是:

<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>

Thoughts anyone? 有人在想吗?

You must create a new Date obj to take the month.Look the code below: 您必须创建一个新的Date obj来获取月份。查看下面的代码:

 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;} 

Your code works if you change: 如果更改,您的代码将起作用:

var month = getMonth();

to

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

However, since the month number matches the option index, you can simply set the select's selectedIndex to the month number: 但是,由于月份号与选项索引匹配,因此您可以简单地将select的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> 

You could also do: 您也可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM