Jquery在单独的选择框中获取年份和月份

时间:2013-09-02 03:07:43

标签: php jquery

我在表格中有D.O.B选择框,我想在选定的年份和月份填写确切的天数

年份:

<select name="yy" id="yy" class="box">
    <option value="2013">2013</option>
    .
    .
    <option value="1955">1955</option>
</select>

月:

<select name="mm" id="mm" class="box">
    <option value="01">01</option>
    .
    .
    <option value="12">12</option>
</select>

我将使用PHP函数填充天数:

function days_in_month($month, $year){
    // calculate number of days in a month
    return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}

在jQuery onchange中,如何将yymm值传递给days_in_month($month, $year),如下所示?

$('#mm').on('change', function() {
    alert( this.value );
});

我不想为每个选定值的更改刷新页面。

1 个答案:

答案 0 :(得分:2)

您可以使用javascript执行此操作,在这种情况下无需使用带有php的ajax:

<script type='text/javascript'>

function days_in_month(month, year){
    // calculate number of days in a month
    return month == 2 ? (year % 4 ? 28 : (year % 100 ? 29 : (year % 400 ? 28 : 29))) : ((month - 1) % 7 % 2 ? 30 : 31);
}

$(document).ready(function() {

 $('#mm').change(function(){

  var mm=$(this).val();//get the month
  var yy=$('#yy').val();//get the day
  $('#dd').val(days_in_month(mm,yy));// i assume that your input for day has id='dd'

 });
});

</script>
相关问题