首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用javascript更改选项标记上的“选定”属性

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

Stack Overflow用户
提问于 2017-06-25 19:51:31
回答 2查看 26关注 0票数 0

我正在为网上订单创建一个网络表单。我希望使用javaScript将复选框默认为当前月份,但它似乎不适合我,我也不知道为什么。我对javascript还不熟悉,但我想我明白了这一点。我是遗漏了什么还是误解了什么?下面是HTML:

代码语言:javascript
运行
复制
<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>

这是我第一次尝试的脚本:

代码语言:javascript
运行
复制
<script>
        var month = getMonth();
        var opt = document.getElementById("m"+month);
        opt.setAttribute("selected");
    </script>

第二种方法是:

代码语言:javascript
运行
复制
<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>

有人在想吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-06-25 20:02:15

您必须创建一个新的Date obj才能接受month.Look (以下代码):

代码语言:javascript
运行
复制
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;}

票数 1
EN

Stack Overflow用户

发布于 2017-06-26 08:35:24

如果您更改了以下代码,代码就会正常工作:

代码语言:javascript
运行
复制
var month = getMonth();

代码语言:javascript
运行
复制
var month = new Date().getMonth();

但是,由于月份号与选项索引匹配,您可以简单地将select的selectedIndex设置为月份号:

代码语言:javascript
运行
复制
window.onload = function() {
  document.getElementById('orderMonth').selectedIndex = new Date().getMonth();
}
代码语言:javascript
运行
复制
<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>

你也可以:

代码语言:javascript
运行
复制
document.getElementById('orderMonth').options[new Date().getMonth()].selected = true;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44750011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档