JavaScript 自定义年月日选择下拉框select选择的日期方法vue实现

Vue 同时被 3 个专栏收录
43 篇文章 2 订阅
46 篇文章 1 订阅
46 篇文章 0 订阅

JavaScript 自定义选择年月日,需求如下图所示:

代码示例如下:

<div class="select-box">
	<span class="item">
		<select v-model="formData.year">
			<option v-for="item in year" :value="item">{{item}}年</option>
		</select>
	</span>
	<span class="item" @change="getDay">
		<select  v-model="formData.month">
			<option v-for="item in month" :value="item">{{item}}月</option>
		</select>
	</span>
	<span class="item">
		<select  v-model="formData.day" >
			<option v-for="item in day" :value="item">{{item}}日</option>
		</select>
    </span>
</div>
<div><a class="btn-submit btn-submit-s" @click="submit"></a></div>

js:

export default {
	data() {
		return {
		        year:[],
			month:[],
			day:[],
			formData:{
				year:2019,
				month:1,
				day:1
			},
		}
	},
	methods: {
		init(){
		  this.getYear();
		  this.getMonth();
		  this.getDay();
		},
		getYear(){//获取年
		  let date = new Date;
		  let current_year = date.getFullYear();
		  for (let i = 0; i < 100; i++) {
		    let y = current_year - i;
			this.year.push(y);
		  }
		},
		getMonth(){//获取月
		  for (let i = 1; i < 13; i++) {
			this.month.push(i);
		  }
		},
		getDay(){//获取日
		  this.day = [];
		  let day = this.getDaysInMonth(this.formData.year, this.formData.month)
		  for (let i = 1; i <= day ; i++) {
		      this.day.push(i);
		  }
		},
		getDaysInMonth(year, month) {//获取某个月的天数
	          month = parseInt(month, 10);
		  let d = new Date(year, month, 0);
		  return d.getDate();
		},
		padding0(num, length) {//数字前填充0方法
		   for(let len = (num + "").length; len < length; len = num.length) {
			  num = "0" + num;            
		   }
		   return num;
		},
                submit(){//提交      
                    let birthday = this.formData.year +"-" + this.padding0(this.formData.month,2) +"-" + this.padding0(this.formData.day,2);

		    let cur_date = new Date();
		    if(cur_date < new Date(birthday)){
			    alert("请选择小于今天的年月日");
			    return;
		    }
                    console.log(birthday);//birthday就是得到的最终的日期,格式为2019-01-01
                }
	 },
	 mounted() {
		this.init();
	 }
}

 

  • 5
    点赞
  • 0
    评论
  • 4
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

<HTML> <HEAD> <title>年月日三联动下拉框</title> <meta http-equiv='Content-Type' content='text/html; charset=gb2312'> <meta name='author' content='http://singlepine.cnblogs.com/'> </HEAD> <body> <form name="form1"> <select name="YYYY"> <option value="" selected>请选择 年</option> </select> <select name="MM"> <option value="" selected>选择 月</option> </select> <select name="DD"> <option value="" selected>选择 日</option> </select> </form> [removed]<!-- function YYYYMMDDstart() { MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; //先给年下拉框赋内容 var y = new Date().getFullYear(); for (var i = (y-30); i < (y+30); i++) //以今年为准,前30年,后30年 document.form1.YYYY.options.add(new Option(" "+ i +" 年", i)); //赋月份的下拉框 for (var i = 1; i < 13; i++) document.form1.MM.options.add(new Option(" " + i + " 月", i)); document.form1.YYYY.value = y; document.form1.MM.value = new Date().getMonth() + 1; var n = MonHead[new Date().getMonth()]; if (new Date().getMonth() ==1 && IsPinYear(YYYYvalue)) n++; writeDay(n); //赋日期下拉框 document.form1.DD.value = new Date().getDate(); } if(document.attachEvent) window.attachEvent("onload", YYYYMMDDstart); else window.addEventListener('load', YYYYMMDDstart, false); function YYYYDD(str) //年发生变化时日期发生变化(主要是判断闰平年) { var MMvalue = document.form1.MM.options[document.form1.MM.selectedIndex].value; if (MMvalue == ""){ var e = document.form1.DD; optionsClear(e); return;} var n = MonHead[MMvalue - 1]; if (MMvalue ==2 && IsPinYear(str)) n++; writeDay(n) } function MMDD(str) //月发生变化时日期联动 { var YYYYvalue = document.form1.YYYY.options[document.form1.YYYY.selectedIndex].value; if (YYYYvalue == ""){ var e = document.form1.DD; optionsClear(e); return;} var n = MonHead[str - 1]; if (str ==2 && IsPinYear(YYYYvalue)) n++; writeDay(n) } function writeDay(n) //据条件写日期下拉框 { var e = document.form1.DD; optionsClear(e); for (var i=1; i<(n+1); i++) e.options.add(new Option(" "+ i + " 日", i)); } function IsPinYear(year)//判断是否闰平年 { return(0 == year%4 && (year0 !=0 || year@0 == 0)); } function optionsClear(e) { e.options.length = 1; } //--> [removed] </body> </HTML>
©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值