對於我正在開發的網站,我使用表單來更新模型的不同字段。要更新的JavaScript結束日期更改開始日期的日期
其中一些字段需要根據其他值進行更新。例如,任務的每個實例都有一個開始日期,結束日期和長度,它們必須驗證以下內容:End = Start + Lenght(週末不算在內,但這是次要問題)。
所以我想寫會做的功能之一:
When the user changes the value of the input Start_date
Get that date from the input field
Get the value of the field Lenght
Update the value of the input "End date" with (Start_date + Lenght)
通過我的例子中涉及的三個輸入有以下ID:id_start,id_lenght,id_finish。
這裏是我寫的,但它在所有不工作(沒有發生明顯的...):
<script>
$("#id_start").change(function() {
var start = new Date();
var finish = new Date();
var numberOfDays = document.getElementById('id_lenght').value;
start = document.getElementById('id_start').value;
finish.setdate(start.getDate()+document.getElementById('id_lenght'))
$('#id_finish').val(finish);
});
</script>
在溶液中的任何暗示,使其工作將非常感激。 在此先感謝。
Knockout.js將是完美的工作。您可以數據綁定元素並定義它們之間的可計算關係。 http://knockoutjs.com/將它與MomentJS結合起來,你也可以輕鬆地進行日期操作。 http://momentjs.com/ – 2014-10-17 12:24:57
如果'$(「#id_start」)'是''使用'keyup'或'blur'事件。 – 2014-10-17 12:28:14
@Burn你的意思是改變事件不適用於輸入元素? – Emeric 2014-10-17 12:30:32