JavaScript用select實現(xiàn)日期控件
更新時間:2015年07月17日 10:46:22 投稿:hebedich
這篇文章主要介紹了JavaScript用select實現(xiàn)日期控件的相關資料,需要的朋友可以參考下
代碼很簡單,這里就不多廢話了,直接給大家源碼吧
<!doctype html>
<html>
<head>
<title>年月日</title>
</head>
<body onLoad="init()">
<select id="year" onChange="swap_day()"></select>年
<select id="month" onChange="swap_day()"></select>月
<select id="day"></select>日
</body>
<script>
var month_big = new Array("1","3","5","7","8","10","12"); //包含所有大月的數(shù)組
var month_small = new Array("4","6","9","11"); //包含所有小月的數(shù)組
//頁面加載時調用的初始化select控件的option的函數(shù)
function init()
{
var select_year = document.getElementById("year"); //獲取id為"year"的下拉列表框
var select_month = document.getElementById("month"); //獲取id為"month"的下拉列表框
var select_day = document.getElementById("day"); //獲取id為"day"的下拉列表框
//將年份選項初始化,從1980到2000
for(var i = 1980; i <= 2000; i++)
{
select_year_option = new Option(i, i);
select_year.options.add(select_year_option);
}
//將月份選項初始化,從1到12
for(var i = 1; i <= 12; i++)
{
select_month_option = new Option(i, i);
select_month.options.add(select_month_option);
}
//調用swap_day函數(shù)初始化日期
swap_day();
}
//判斷數(shù)組array中是否包含元素obj的函數(shù),包含則返回true,不包含則返回false
function array_contain(array, obj)
{
for (var i = 0; i < array.length; i++)
{
if (array[i] === obj)
{
return true;
}
}
return false;
}
//根據(jù)年份和月份調整日期的函數(shù)
function swap_day()
{
var select_year = document.getElementById("year"); //獲取id為"year"的下拉列表框
var select_month = document.getElementById("month"); //獲取id為"month"的下拉列表框
var select_day = document.getElementById("day"); //獲取id為"day"的下拉列表框
select_day.options.length = 0; //在調整前先清空日期選項里面的原有選項
var month = select_month.options[select_month.selectedIndex].value; //獲取被選中的月份month
//如果month被包含在month_big數(shù)組中,即被選中月份是大月,則將日期選項初始化為31天
if(array_contain(month_big, month))
{
for(var i = 1; i <= 31; i++)
{
select_day_option = new Option(i, i);
select_day.options.add(select_day_option);
}
}
//如果month被包含在month_small數(shù)組中,即被選中月份是小月,則將日期選項初始化為30天
else if(array_contain(month_small, month))
{
for(var i = 1; i <= 30; i++)
{
select_day_option = new Option(i, i);
select_day.options.add(select_day_option);
}
}
//如果month為2,即被選中的月份是2月,則調用initFeb()函數(shù)來初始化日期選項
else
{
initFeb();
}
}
//判斷年份year是否為閏年,是閏年則返回true,否則返回false
function isLeapYear(year)
{
var a = year % 4;
var b = year % 100;
var c = year % 400;
if( ( (a == 0) && (b != 0) ) || (c == 0) )
{
return true;
}
return false;
}
//根據(jù)年份是否閏年來初始化二月的日期選項
function initFeb()
{
var select_year = document.getElementById("year"); //獲取id為"year"的下拉列表框
var select_day = document.getElementById("day"); //獲取id為"day"的下拉列表框
var year = parseInt(select_year.options[select_year.selectedIndex].value); //獲取被選中的年份并轉換成Int
//如果是閏年,則將日期選項初始化為29天
if(isLeapYear(year))
{
for(var i = 1; i <= 29; i++)
{
select_day_option = new Option(i, i);
select_day.options.add(select_day_option);
}
}
//如果不是閏年,則將日期選項初始化為28天
else
{
for(var i = 1; i <= 28; i++)
{
select_day_option = new Option(i, i);
select_day.options.add(select_day_option);
}
}
}
</script>
</html>
以上所述就是本文的全部內容了,希望大家能夠喜歡。
相關文章
詳解js對象中屬性的兩種類型之數(shù)據(jù)屬性和訪問器屬性
在理解vue底層響應式原理時,了解到,原來對象中的屬性,不單單從表面看起來那么簡單是key:value形式,而是還有隱藏的內部特性,其中對象內的屬性分為兩種類型的屬性:數(shù)據(jù)屬性和訪問器屬性,本文將給大家詳細介紹一下數(shù)據(jù)屬性和訪問器屬性,需要的朋友可以參考下2023-05-05
JavaScript統(tǒng)計網(wǎng)站訪問次數(shù)的實現(xiàn)代碼
每一個稱職的網(wǎng)管,都需要知道每天網(wǎng)站的訪問量,需要實現(xiàn)網(wǎng)站訪問次數(shù)功能來滿足需求,本篇文章主要介紹了JavaScript統(tǒng)計網(wǎng)站訪問次數(shù)的實現(xiàn)代碼,感興趣的小伙伴們可以參考一下2015-11-11
解決使用bootstrap的dropdown部件時報錯:error:Bootstrap dropdown require
這篇文章主要介紹了使用bootstrap的dropdown部件時報錯:error:Bootstrap dropdown require Popper.js ,小編把問題描述和解決方案分享給大家,需要的朋友可以參考下2018-08-08

