自动填充第二个日期字段无法正常工作 - jQuery datepicker

时间:2016-04-27 14:39:24

标签: javascript jquery date autocomplete datepicker

我正在尝试自动填充" end_date"字段基于用户在"天"中输入的天数领域。例如,用户输入2天的字段。然后用户继续选择开始日期为2016年4月27日。结束日期应填写2016年4月29日。我对javascript不太熟悉,我花了几个小时搜索但到目前为止还没有答案。任何帮助,将不胜感激。

<html>
<head>
<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>
</head>
</html>

<input id='days' class='days' type='text'/>
<input id='start_date' class='start_date' type='text'/>
<input id='end_date' class='end_date' type='text'/>


<script>
$(function() {
$(".days").val();
$(".end_date").datepicker();
$(".start_date").datepicker({
onSelect:function(){
var x = $(".days").val();
var toDate = $(this).datepicker('getDate');
toDate.setDate(toDate.getDate() + x)
$(".end_date").datepicker("setDate", toDate);
}
});
});
</script>

1 个答案:

答案 0 :(得分:0)

我一直在尝试一些事情,并且能够找到使这段代码有效的方法。这可能不是最好的方法,但它适用于我想要做的事情。我将这行代码从toDate.setDate(toDate.getDate()+ x)改为toDate.setDate(toDate.getDate()+(x / 1))。

<html>
<head>
<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>
</head>
</html>

<input id='days' class='days' type='text'/>
<input id='start_date' class='start_date' type='text'/>
<input id='end_date' class='end_date' type='text'/>


<script>
$(function() {
$(".on_site_days").val();
$(".audit_end_date").datepicker();
$(".audit_start_date").datepicker({
onSelect:function(){

dateFormat : 'dd-mm-yy'

var x = $(".on_site_days").val();
var toDate = $(this).datepicker('getDate');
toDate.setDate(toDate.getDate() + (x / 1))
$(".audit_end_date").datepicker("setDate", toDate);
}
});
});
</script>