假设现有两个列表框month、days
现在想用JavaScript判断month的值如果为1,3,5......等大月,days内就显示1-31,否则就30,2月除外!
求用JavaScript实现一个小功能
答案:4 悬赏:70 手机版
解决时间 2021-03-28 18:25
- 提问者网友:寂寞撕碎了回忆
- 2021-03-28 00:34
最佳答案
- 五星知识达人网友:十鸦
- 2020-02-02 21:33
<script language=javascript>
function days(obj,obj2){
var pro=document.getElementById(obj).value;
var day31=",1,3,5,7,8,10,12,";
var day30=",4,6,9,11,";
var pro2=","+pro+",";
var n=0;
document.getElementById(obj2).length=1;
if(day31.indexOf(pro2)>-1){n=31;}
if(day30.indexOf(pro2)>-1){n=30;}
if(pro==2){n=28;}
for(var i=0;i<n;i++){
document.getElementById(obj2).options[document.getElementById(obj2).length]=new Option(i+1,i+1);}
}
</script>
<select name="month" id="month" size=1 onchange="days('month','days')">
<option value="">-选择月-</option>
<script language=javascript>
for(var i=1;i<=12;i++){
document.write("<option value="+i+">"+i+"</option>");
}
</script>
</select>
<select name="days" id="days" size=1>
<option value="">-选择日-</option>
</select>
2级联动
function days(obj,obj2){
var pro=document.getElementById(obj).value;
var day31=",1,3,5,7,8,10,12,";
var day30=",4,6,9,11,";
var pro2=","+pro+",";
var n=0;
document.getElementById(obj2).length=1;
if(day31.indexOf(pro2)>-1){n=31;}
if(day30.indexOf(pro2)>-1){n=30;}
if(pro==2){n=28;}
for(var i=0;i<n;i++){
document.getElementById(obj2).options[document.getElementById(obj2).length]=new Option(i+1,i+1);}
}
</script>
<select name="month" id="month" size=1 onchange="days('month','days')">
<option value="">-选择月-</option>
<script language=javascript>
for(var i=1;i<=12;i++){
document.write("<option value="+i+">"+i+"</option>");
}
</script>
</select>
<select name="days" id="days" size=1>
<option value="">-选择日-</option>
</select>
2级联动
全部回答
- 1楼网友:我住北渡口
- 2020-10-07 11:47
呵呵,网上一大堆!
- 2楼网友:梦中风几里
- 2020-04-20 07:06
<
- 3楼网友:天凉才是好个秋
- 2019-09-19 07:57
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<select id="mons"></select>
<select id="days"></select>
<script language="javascript" type="text/javascript">
function $(o){return document.getElementById(o)}
var mstr=[];
var bigmonth=[1,3,5,7,8,10,12];
fopts($('mons'),'月',12);fopts($('days'),'日',31);
$('mons').onchange=function(){
var cMon=this.value;
if(isin(bigmonth,cMon)){
fopts($('days'),'日',31);
}else{//如果要判断2月的日数,需要加入年份才能判断
fopts($('days'),'日',30);
}
}
function fopts(obj,str,n){
for(var i=1;i<=n;i++){
var tmpopt=document.createElement('option');
tmpopt.text=i+str;
tmpopt.value=i;
try{obj.add(tmpopt,null)}catch(ex){obj.add(tmpopt)}
}
}
function isin(arr,str){
for(var i=0;i<arr.length;i++){
var rValue=false;
if(arr[i]==str){rValue=true;break}else{rValue=false;continue;}
}
return rValue;
}
</script>
</body>
</html>
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯