我想使用 jQuery ajax 获取每月的天数,但是 .change 事件不起作用。你知道为什么吗?
我尝试调用 alert() 函数,但没有响应。
索引.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;
?>