2013-05-01 54 views
0

我在嘗試訪問JavaScript腳本內部的函數時遇到問題,因此我可以根據選擇的月份創建一組動態的可能日期。它甚至不會訪問JavaScript函數'populate()'。警報不響應。我應該提到這個完整的代碼是從'include_once();'在另一個文件中。我試圖按照從例如:http://www.youtube.com/watch?feature=player_embedded&v=UliJeDbc4cw動態日期選擇(Php/JS)

<script type="text/javascript"> 
function populate(month,day){ 
    alert('Javascript Reached'); 
    var month = document.getElementById('month'); 
    var day = document.getElementById('day'); 

    day.innerHTML=""; 

    if (month.value == "Apr" || 
     month.value == "Sep" || 
     month.value == "Jun" || 
     month.value == "Nov" || 
     ){ 
     var limit = 30; 
     } 
    else if(month.value =="feb"){ 
     var limit = 28; 
    } 

    else{ 
     var limit = 31; 
    } 

    while(var temp < limit){ 
     var newOption = document.createElement("option"); 
     newOption.value = temp; 
     newOption.innerHTML = temp; 
     day.options.add(newOption); 
     temp++; 
    } 

    return true; 
} 
</script> 
<?php 
adminYearOption(); 
adminMonthOption(); 
adminDayOption(); 

function adminYearOption(){ 
    echo "<select name='year'><option value=''></option>"; 
    while($datecount < 50){ 
     $currValue = (1980+$datecount); 
     echo "<option value='".$currValue."'>".$currValue."</option>"; 
     $datecount++; 
    } 
    echo "</select>"; 
} 

function adminMonthOption(){ 
    ?> <select id='month' name='month' onchange="return populate(this.id,'day')"><option value=''></option><?php 
    $time = strtotime("2013-01-01"); 
    while($datecount < 12){ 
     $currValue = date('M',$time); 
     echo "<option value='".$currValue."'>".$currValue."</option>"; 
     $time = strtotime("+1 month", $time); 
     $datecount++; 
    } 
    echo "</select>"; 
} 

function adminDayOption(){ 
    echo "<select id='day' name='day'></select>"; 
} 
?> 

'

也爲想要創建動態的日期,這裏的任何其他閱讀器是一種替代方案: http://blog.elanman.com/2009/09/create-dynamic-date-selects/但它有天的靜態量爲每月。

我只是好奇爲什麼我沒有工作,我不知道。

謝謝。

回答

2

您可能還想考慮閏年,這會改變2月份的天數。

首先創建列表:

<select name="month"> 
    <option value="1">January</option> 
    <option value="2">February</option> 
    <option value="3">March</option> 
    <option value="4">April</option> 
    <option value="5">May</option> 
    <option value="6">June</option> 
    <option value="7">July</option> 
    <option value="8">August</option> 
    <option value="9">September</option> 
    <option value="10">October</option> 
    <option value="11">November</option> 
    <option value="12">December</option> 
</select> 
<select name="day"></select> 
<select name="year"></select> 

然後執行此創建的JavaScript:

var ysel = document.getElementsByName("year")[0], 
    msel = document.getElementsByName("month")[0], 
    dsel = document.getElementsByName("day")[0]; for (var i = 2014; i>=1950; i--){ 
    var opt = new Option(); 
    opt.value = opt.text = i; 
    ysel.add(opt); } ysel.addEventListener("change",validate_date); msel.addEventListener("change",validate_date); 

function validate_date(){ 
    var y = +ysel.value, m = msel.value, d = dsel.value; 
    if (m === "2") var mlength = 28 + (!(y & 3) && ((y % 100)!==0 || !(y & 15))); 
    else var mlength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][m - 1]; 
    dsel.length = 0; 
    for(var i=1;i<=mlength;i++){ var opt=new Option(); opt.value = opt.text = i; if(i==d) opt.selected=true;  dsel.add(opt); 
    } } validate_date(); 

它完成。你現在有一個動態的日期列表,也考慮閏年。
提示:我把</html>後面的javascript放到<head></head>裏面,因爲它不起作用。