簡體   English   中英

Javascript 日期選擇器

[英]Javascript Date selector

我需要用戶 select 他/她的出生日期,我正在使用 javascript 和 php。

    var date_arr = new Array;
var days_arr = new Array;

date_arr[0]=new Option("January",31);
date_arr[1]=new Option("February",28);
date_arr[2]=new Option("March",31);
date_arr[3]=new Option("April",30);
date_arr[4]=new Option("May",31);
date_arr[5]=new Option("June",30);
date_arr[6]=new Option("July",31);
date_arr[7]=new Option("August",30);
date_arr[8]=new Option("September",30);
date_arr[9]=new Option("October",31);
date_arr[10]=new Option("November",31);
date_arr[11]=new Option("December",30);

function fill_select(f)
{
        document.writeln("<SELECT name=\"months\"               onchange=\"update_days(FRM)\">");
        for(x=0;x<12;x++)
                document.writeln("<OPTION value=\""+date_arr[x].value+"\">"+date_arr[x].text);
        document.writeln("</SELECT><SELECT name=\"days\"></SELECT>");
        selection=f.months[f.months.selectedIndex].value;
}

function update_days(f)
{
        temp=f.days.selectedIndex;
        for(x=days_arr.length;x>0;x--)
        {
                days_arr[x]=null;
                f.days.options[x]=null;
         }
        selection=parseInt(f.months[f.months.selectedIndex].value);
        ret_val = 0;
        if(f.months[f.months.selectedIndex].value == 28)
        {
                year=parseInt(f.years.options[f.years.selectedIndex].value);
                if (year % 4 != 0 || year % 100 == 0 ) ret_val=0;
                else
                        if (year % 400 == 0)  ret_val=1;
                        else
                                ret_val=1;
        }
        selection = selection + ret_val;
        for(x=1;x < selection+1;x++)

        {
                days_arr[x-1]=new Option(x);
                f.days.options[x-1]=days_arr[x-1];
        }
        if (temp == -1) f.days.options[0].selected=true;
        else
             f.days.options[temp].selected=true;
}
function year_install(f)
{
        document.writeln("<SELECT name=\"years\" onchange=\"update_days(FRM)\">")
        for(x=1950;x<2005;x++) document.writeln("<OPTION value=\""+x+"\">"+x);
        document.writeln("</SELECT>");
        update_days(f)
}

這是生成代碼:

<script>fill_select(document.frmProfile);year_install(document.frmProfile)</script>

問題是當它提交到數據庫時,日期和年份是根據選擇發送的,但是月份顯示的是該月的總天數而不是月份本身。

例如:-- 如果我想在 1996 年 2 月 20 日 select 我想得到一個 output 1996-02-20 進入數據庫。 使用此代碼我得到一個 output 作為 1996-28-20 我知道問題出在這個 function

function fill_select(f)
    {
            document.writeln("<SELECT name=\"months\"               onchange=\"update_days(FRM)\">");
            for(x=0;x<12;x++)
                    document.writeln("<OPTION value=\""+date_arr[x].value+"\">"+date_arr[x].text);
            document.writeln("</SELECT><SELECT name=\"days\"></SELECT>");
            selection=f.months[f.months.selectedIndex].value;
    }

但我只是不知道我到底應該改變什么

您可能希望避免計算閏年的麻煩,並改用jQuery UI 日期選擇器

因為當您點擊“提交”時會使用選項的值,所以您不希望月份值包含它們的天數(28、30、31),而是包含它們的實際數字(1-12)。

所以改變

for(x=0;x<12;x++)
    document.writeln("<OPTION value=\""+date_arr[x].value+"\">"+date_arr[x].text);

...進入...

for(x=0;x<12;x++)
    document.writeln("<OPTION value=\""+(x+1)+"\">"+date_arr[x].text);

現在我們不能再依賴這些值來確定一個月有多少天,但我們可以使用date_arr這件事。 所以改變:

selection=parseInt(f.months[f.months.selectedIndex].value);
ret_val = 0;
if(f.months[f.months.selectedIndex].value == 28)
{ // (...)

... 進入...

selection=parseInt(date_arr[f.months.selectedIndex].value);
ret_val = 0;
if(selection == 28)
{ // (...)

現在它應該工作得更好。 祝你好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM