2016-04-27 36 views
0

我想根据用户输入“天”字段的天数自动填充“end_date”字段。例如,用户在天数字段中输入2。然后用户选择开始日期04/27/2016。结束日期应填写2016年4月29日。我对JavaScript并不是很熟悉,我花了几个小时搜索,但目前为止还没有答案。任何帮助,将不胜感激。自动填充第二个日期字段工作不正常 - jQuery datepicker

<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> 
+0

使用此代码,而不是你的代码: 变种X = $( “#天”)VAL(); – Dilip

+0

检查它,并让我知道 – Dilip

+0

@Dilip我只是试过了,仍然没有运气。它显示错误的日期。如果我输入1天和4/01/2016开始,结束日期显示4/11/2016。也许某种格式问题? –

回答

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> 
相关问题