首页 > PHP教程 > php开发知识文章

PHP 日期选择器实现(年月日三级联动)

本文主要介绍了PHP 日期选择器实现(年月日三级联动)的实现代码,需要的朋友参考下

本文的年月日的三级联动主要是用的select标签

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>日期选择</title>
</head>
<body>
<input type="text" id="hs"> <input type="button" id="btn" value="提交">
<div style="margin-top: 19px;">日期选择:<span id="rent"></span></div>

<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
var str = "<select id='year'></select> <select id='month'>" + "</select> <select id='day'></select>";
$("#rent").html(str);
fullyear();
fullmonth();
fullday();
//当其中一个改变,后面的要跟着改变
$("#year").change(function () {
fullday();
});
$("#month").change(function () {
fullday();
});
function fullyear() {
var d = new Date();
var year = d.getFullYear();
str = "";
for (var i = year - 5; i < year + 6; i++) {
if (i == year) {
str += "<option selected='selected' value='" + i + "'>" + i + "</option>";
} else {
str += "<option value='" + i + "'>" + i + "</option>"
}
}
$("#year").html(str);
}
function fullmonth() {
var d = new Date();
var month = d.getMonth() + 1;
str = "";
for (var j = 1; j < 13; j++) {
if (j == month) {
str += "<option selected='selected' value='" + j + "'>" + j + "</option>";
} else {
str += "<option value='" + j + "'>" + j + "</option>"
}
}
$("#month").html(str);
}
function fullday() {
var d = new Date();
var day = d.getDate();
var year = $("#year").val();
var month = $("#month").val();
var rq = 31;
str = "";
if (month == 4 || month == 6 || month == 9 || month === 11) {
rq = 30;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
rq = 29;//闰年
} else {
rq = 28; //不是闰年
}
}
for (var n = 1; n < rq + 1; n++) {
if (n == day) {
str += "<option selected='selected' value='" + n + "'>" + n + "</option>";
} else {
str += "<option value='" + n + "'>" + n + "</option>";
}
}
$("#day").html(str);
}
//把内容存到表单中
$("#btn").click(function () {
var nian = $("#year").val();
var yue = $("#month").val();
var ri = $("#day").val();
var time = nian + "-" + yue + "-" + ri + "";
$("#hs").val(time)
})
</script>
</body>
</html>

以上就是本文PHP 日期选择器实现(年月日三级联动)的全部内容,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

PHP 日期选择器实现(年月日三级联动)

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。