我正在使用 Bootstrap DatePicker:https ://github.com/eternicode/bootstrap-datepicker
我将它附加到两个盒子 - InvDate 和 InvDueDate - 它弹出来没问题,并设置日期等就好了。
I have a drop down box though, that when selected, I want to take the date in the InvDate box, and add the val() of the dropdownlist to the InvDate, and populate InvDueDate.
但是,我似乎无法让 InvDueDate 更改它的日期(什么都不做) - 下拉框确实触发了该功能,但这条线似乎没有做任何事情:
$("#InvDueDate").datepicker("setDate", duedate);
:
HTML是:
<div class="editor-label">
<label for="InvDate">InvDate</label>
</div>
<div class="editor-field">
<input class="textbox datepicker" data-val="true" data-val-date="The field InvDate must be a date." data-val-required="The InvDate field is required." id="InvDate" name="InvDate" type="text" value="02 May 2013" />
<span class="field-validation-valid" data-valmsg-for="InvDate" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="Terms">Terms</label>
</div>
<div class="editor-field">
<select id="Terms" name="Terms"><option value="0">Due on invoice date</option>
<option value="7">Net 7 days</option>
<option value="14">Net 14 days</option>
<option value="21">Net 21 days</option>
<option value="28">Net 28 days</option>
<option value="45">Net 45 days</option>
<option value="60">Net 60 days</option>
</select>
</div>
<div class="editor-label">
<label for="InvDueDate">InvDueDate</label>
</div>
<div class="editor-field">
<input class="textbox datepicker" data-val="true" data-val-date="The field InvDueDate must be a date." data-val-required="The InvDueDate field is required." id="InvDueDate" name="InvDueDate" type="text" value="02 May 2013" />
<span class="field-validation-valid" data-valmsg-for="InvDueDate" data-valmsg-replace="true"></span>
</div>
我的 jQuery/Javascript 是:
<script type="text/javascript">
$(document).ready(function () {
$("#Terms").change(function () {
var invdate = new Date();
var duedate = new Date();
invdate = $("#InvDate").val();
var net = $("#Terms").val();
duedate.setDate(invdate.getDate() + net);
$("#InvDueDate").datepicker("setDate", duedate);
});
});
</script>
我出错的地方对任何人来说都很明显吗?
更新以显示工作代码
var start_date = new Date($('#InvDate').val());
var duration = parseInt($('#Terms').val());
var end_date = new Date();
end_date.setDate(start_date.getDate() + duration);
$("#InvDueDate").datepicker("setDate", end_date);