Dynamic Date Select (Php/JS)

╄→尐↘猪︶ㄣ 提交于 2019-12-12 01:07:59

问题


I'm having problems trying to access a function inside a JavaScript script, so I can create a dynamic set of possible dates based on what month is selected. It doesn't even seem to access the JavaScript function 'populate()' at all. The alert doesn't respond. I should probably mention this entire code is called from 'include_once();' in another file. I tried to follow the example from: http://www.youtube.com/watch?feature=player_embedded&v=UliJeDbc4cw

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

'

Also for any other reader that wants to create dynamic dates, here is an alternative: http://blog.elanman.com/2009/09/create-dynamic-date-selects/ but it has a static amount of days for every month.

I'm just curious as to why mine didn't work, I cant figure it out.

Thanks.


回答1:


You might also want to consider leap years, which changes the number of days for February.

Create first the list:

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

Then create the javascript with this:

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();

And it's done. You now have a dynamic date list which also considers leap year.
Tip: I put the javascript after the </html> because when I try to put it inside the <head></head>, it's not working.



来源:https://stackoverflow.com/questions/16316592/dynamic-date-select-php-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!