2013-01-21 128 views
0

我想使用jQuery ajax獲取月份中的天數,但.change事件不起作用。你有什麼想法,爲什麼?爲什麼.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; 

?> 

回答

0

現在給你的代碼是完美的。你在哪裏添加了<script>標籤來加載jquery。我認爲這是你的情況造成的問題。

這裏是一個工作示例http://jsfiddle.net/dvfEX/

+0

的jquery.js包括 –

+0

我已經解決了它。 // <![CDATA [/ code] $(window).load(function(){在腳本開始時需要。 –

相關問題