gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:00:09 24 4
gpt4 key购买 nike

我正在为在线订单创建一个网络表单。我想使用 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>

有人有什么想法吗?

最佳答案

您必须创建一个新的 Date 对象来获取月份。请看下面的代码:

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

关于javascript - 如何使用 javascript 更改选项标签上的 "selected"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44750011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com