2012-08-09 64 views
0

我想在选择更改时重新填充选择框。使用循环填充选择

我做了什么至今(不工作):

$('select#mnt').change(function(){ 

    var month = $(this).val(); 
    var days = new Date(year, month, 0).getDate(); 
    var toMonth = <?php echo $tomon; ?>; 
    var toDay = <?php echo $today; ?>; 
    var result=''; 

    if (month!=toMonth) { 

     for(var i=1; i<=days;i++){ 

      result += '<option>'+i+'</option>'; 

     } 

     $('select#days').empty().html(result); 
    } 




    }); 
+0

脚本在哪里不起作用? – rollstuhlfahrer 2012-08-09 08:53:07

回答

1

您没有在该函数的变量year。你在别处创建它吗?

这里的工作示例,有了一些变化:DEMO

$('select#mnt').change(function(){ 

var month = $(this).val(); 
var days = new Date(2012, month, 0).getDate(); 
var toMonth = 5; 
var toDay = 4; 
var result=''; 

if (month!=toMonth) { 

    for(var i=1; i<=days;i++){ 

     result += '<option>'+i+'</option>'; 
    } 

    $('select#days').empty().html(result); 
} 
}); 
+0

谢谢,我完全忽略了年变量;) – John 2012-08-09 09:03:29

2

year VAR不会在您的代码段存在:

var days = new Date(year, month, 0).getDate(); 

应该是这样的:

var year=2011;//or $('input#year').val(); 
var days = new Date(year, month, 0).getDate();