首页 >web前端 >js教程 >js日期联动示例_javascript技巧

js日期联动示例_javascript技巧

WBOY
WBOY原创
2016-05-16 16:50:111286浏览

调试需加入jquery文件

复制代码 代码如下:





<script> <BR>$(function(){ <BR>$('#year').change(function(){ <BR>if (ifLeapYear($(this).val())) <BR>{ <BR>if ($('#month').val() == 2 && $('#day').children().length == 29) <BR>{ <BR>$('#day').append('<option value="29">29'); <BR>} <BR>} else <BR>{ <BR>if ($('#month').val() == 2 && $('#day').children().length == 30) <BR>{ <BR>$('#day :last-child').remove(); <BR>} <BR>} <BR>}); <br><br>$('#month').change(function(){ <BR>var thisValue = $(this).val(); <BR>var dayValue = $('#day').val(); <BR>var month1 = ['4','6','9','11']; <BR>$('#day').html('<option value="">-请选择-'); <BR>var day = ''; <BR>if (thisValue == '') <BR>{ <BR>return false; <BR>} <BR>if ($.inArray(thisValue, month1) != -1) <BR>{ <BR>day = setDay(30); <BR>$('#day').append(day); <BR>} else if ($(this).val() == '2') <BR>{ <BR>if ($('#year').val() != '' && ifLeapYear($('#year').val())) <BR>{ <BR>day = setDay(29); <BR>} else <BR>{ <BR>day = setDay(28); <BR>} <BR>$('#day').append(day); <BR>} else <BR>{ <BR>day = setDay(31); <BR>$('#day').append(day); <BR>} <BR>$('#day').find('option[value='+dayValue+']').attr('selected',true); <BR>}); <BR>}) <BR>function setDay(day) <BR>{ <BR>var dayInfo = ''; <BR>for (var i=1; i<=day; i++) <BR>{ <BR>dayInfo += '<option value="' + i +'">' + i+ ''; <BR>} <BR>return dayInfo; <BR>} <BR>function ifLeapYear(year) <BR>{ <BR>if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) <BR>{ <BR>return true; <BR>} else <BR>{ <BR>return false; <BR>} <BR>} <BR></script>



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn