【问题标题】:Auto fill for 2nd date field not working properly - jQuery datepicker第二个日期字段的自动填充无法正常工作 - jQuery datepicker
【发布时间】:2016-04-27 14:39:23
【问题描述】:

我正在尝试根据用户在“天数”字段中输入的天数自动填充“结束日期”字段。例如,用户在天字段中输入 2。用户然后继续选择 2016 年 4 月 27 日的开始日期。结束日期应填写 04/29/2016。我对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>

【问题讨论】:

  • 使用此代码代替您的代码:var x = $("#days").val();
  • 检查并告诉我
  • @Dilip 我刚刚尝试过,但仍然没有运气。它显示错误的日期。如果我输入 1 天和 2016 年 4 月 1 日开始,结束日期显示 2016 年 4 月 11 日。也许是某种格式问题?

标签: javascript jquery date autocomplete datepicker


【解决方案1】:

我一直在尝试一些事情,并且能够找到一种方法来使这段代码工作。这可能不是最好的方法,但它适用于我正在尝试做的事情。我将这行代码从 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>

【讨论】:

    猜你喜欢
    • 2020-04-27
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 2022-01-19
    • 2021-06-16
    • 2018-06-09
    • 2012-01-17
    相关资源
    最近更新 更多