通过选择从开始日期开始的周数或月数来自动结束日期

时间:2016-03-06 10:01:25

标签: javascript php jquery joomla

我已经提交了几周的文本,并且我有日期选择器来选择开始日期。

我如何根据我选择的周数结束日期?

如果我有几个月的文本字段并根据选择开始日期获得结束日期怎么办?

我尝试了这个,但它不起作用:

<script type="text/javascript">
jQuery(document).ready(function($) {
 $('#start_date').bind('change', function() {
   var days = $('#week_number').val() * 7;
  var endDate = new Date($(this).val());
  endDate.setDate(endDate.getDate() + days);
  $('#end_week_return').val( (endDate.getMonth() + 1)+ '/' + endDate.getDate() + '/' + endDate.getFullYear() );
});
});
</script>

1 个答案:

答案 0 :(得分:1)

以上代码似乎没问题。问题将出现在页面的其他部分,可能在您的标头js文件或html元素中。我已经创建了一个示例html文件,请尝试这个,它可能会对你有帮助。

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$( "#start_date" ).datepicker();
$('#start_date').bind('change', function() {
var days = $('#week_number').val() * 7;
var endDate = new Date($(this).val());
endDate.setDate(endDate.getDate() + days);
$('#end_week_return').val( (endDate.getMonth() + 1)+ '/' +    endDate.getDate() + '/' + endDate.getFullYear() );
 });
});
</script>
</head>

<body>
  No of weeks: <input type="text" id="week_number" />
  Start date: <input type="text" id="start_date" />
  End date:<input type="text" id="end_week_return" />
</body>