2016-04-27 40 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> 
相關問題