2011-06-03 85 views
0

我需要用戶選擇他/她的出生日期,我正在使用javascript & php。Javascript日期選擇器

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> 

問題是,當它提交到數據庫的日和年是根據選擇發送,但當月顯示的總天NUM在這一個月而不是月份本身。例如: - 如果我想選擇1996年2月20日我想獲得輸出1996-02-20進入數據庫。 使用此代碼,我得到一個輸出爲1996-28-20 我知道這個問題是在這個函數

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; 
    } 

但我不知道我應該改變什麼

+0

使用螢火蟲或調試器或DOM查看器,看看有什麼是你的選擇裏面的值時,頁面呈現 – Ibu 2011-06-03 05:19:21

+0

在Firebug: - <期權價值=「31」>一月 <期權價值=「28」 >二月 <期權價值=「31」>三月 月是用個月的價值> 的總NUM但我不知道如何在代碼中改變這種情況。 (x = 0; x <12; x ++) document.writeln(「

+0

按下你的問題上的編輯按鈕,並添加你想讓你的代碼生成的格式樣本 – Ibu 2011-06-03 06:48:47

回答

0

您可能要避免計算閏年和所有的麻煩,並使用the jQuery UI datepicker代替。

由於選項的值在您點擊'提交'時使用,您不希望月份值包含其天數(28,30,31),而是其實際數量(1-12) 。

因此改變

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

... INTO ...

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) 
{ // (...) 

... INTO ...

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

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

+0

非常感謝michel404。有效 。 我沒有簽出jQuery UI datepicker,非常酷。 – tycoon 2011-06-03 10:52:13