本文实例为大家分享了jQuery实现只显示年份下拉框的具体代码,供大家参考,具体内容如下
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >Document</ title >
< script src = "https://lib.baomitu.com/jquery/3.5.1/jquery.js" ></ script >
</ head >
< body >
< div id = "main" style = "width: 600px;height:400px;" ></ div >
< select name = "" id = "start" onchange = "chooseYear2()" >
< option value = "" >1949</ option >
</ select >
< select name = "" id = "end" >
< option value = "" ></ option >
</ select >
< script type = "text/javascript" >
var date = new Date();
var y = date.getFullYear()
function chooseYear1() {
for (let i = 1949; i<=y; i ++) {
let start = document.getElementById("start");
let year = Number(start.lastElementChild.innerHTML) + 1;
var myOption = document.createElement("option");
myOption.innerHTML = year;
start.appendChild(myOption);
}
}
chooseYear1();
function chooseYear2() {
//如果你想让起始年份下拉框和结束年份下拉框都显示从1949至今的年份,那么写同样的循环即可:
let end = Number($("#start").find("option:selected").val());
let start = document.getElementById("end");
start.firstElementChild.innerHTML=end;
var myOption = document.createElement("option");
for (let i = end; i<=y; i ++) {
let start = document.getElementById("end");
let year = Number(start.lastElementChild.innerHTML) + 1;
var myOption = document.createElement("option");
myOption.innerHTML = year;
start.appendChild(myOption);
}
}
</ script >
</ body >
</ html >
|
只显示年份的下拉框效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_42952297/article/details/111567326