0

jQuery ajaxで月の日数を取得したいのですが、.changeイベントがうまくいきません。理由はわかりますか?

alert() 関数を呼び出そうとしましたが、応答がありませんでした。

index.html

 <script src="js/jquery.js"></script>
 <script type="text/javascript">
   $("#month").change(function() {

       alert("Alert something!");

       var year= $("#year").val();
       var month= $("#month").val();

       $.ajax({
            type: "POST",
            url: "getMonthDays.php",
            dataType: "html",   //expect html to be returned
            data: {year: year, month: month}
            }).done(function(msg){
                if(parseInt(msg)!=0)    //if no errors
                {
                    $('#days').html(msg);    //load the returned html into days
                }
            });

    });

  </script>

    <select id="year">
        <option value="2012">2012</option>
        <option value="2013">2013</option>
    </select>

    <select id="month">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>

    <select id="day">
        <div id="daysInMonth"></div>
    </select>

getDaysInMonth.php

<?php

    function getDaysInMonth($month, $year){
    return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}

    $year = $_POST["year"];
    $month = $_POST["month"];

    $daysInMonth = getDaysInMonth($month, $year);

    $string = "<option value=\"0\">Den:</option>";

    for($i = 1; $i <= $daysInMonth; $i++){
        $string .= "<option value=\"$i\">$i</option>";
    }

    echo $string;

?>
4

1 に答える 1

0

コードは完璧です。<script>jqueryをロードするためのタグをどこに追加しましたか。これはあなたの場合に問題を引き起こしていると思います。

ここに1つの実例がありますhttp://jsfiddle.net/dvfEX/

于 2013-01-21T12:51:43.463 に答える