0
如果另一個字段的值發生更改,我需要更新一個字段的值。我想要更新的字段包含時間戳,並且其值可能更改的字段包含日期(yyyy-MM-dd)。通過日曆選擇器進行更改,這可能是onchange
和oninput
事件無法調用該函數的原因。使用日曆選擇器更新另一個onchange輸入值
這裏是我的代碼:
<input required class="short" type="text" id="startdate" name="startdate" value="<?php echo (isset($startdate) ? $startdate : "") ?>" oninput="updateTimestamp();" />
<input required class="short" type="text" id="startdate_u" name="startdate_u" value="<?php echo (isset($startdate_u) ? $startdate_u : "") ?>" />
<a href="#" onclick="cal.select(document.forms['project'].startdate,'anchor','yyyy-MM-dd'); return false;" name="anchor" id="anchor"><img src="images/calendar_icon.png" /></a>
<script>
var cal = new CalendarPopup();
function popup(mylink, windowname) {
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
href=mylink;
else
href=mylink.href;
window.open(href, windowname, 'width=500,height=300,scrollbars=yes,resizable=yes');
return false;
}
function updateTimestamp() {
var startDate = document.getElementById("startdate");
var newTimestamp = Math.round(new Date(startDate.value).getTime()/1000);
document.getElementById("startdate_u").value = newTimestamp.toString();
}
</script>
這裏的日曆代碼的鏈接:http://www.mattkruse.com/javascript/calendarpopup/source.html
我沒有使用JQuery。 您的建議是非常感謝:) 整個網站是根據HTML5標準寫 - 我讀oninput
應該做的伎倆,然後它不。在Chrome和IE 11上測試。
對於'startDate'和'Math.round(new Date(startdate)'這個變量沒有大寫的** D **,你沒有得到'.value''嘗試'Math。 (新的日期(startDate.value).getTime()/ 1000);' – NewToJS
你是對的,我只是輸入這些解釋錯別字,早些時候我用'.value'和兩個都試過,甚至沒有一個簡單的' alert()'被觸發 – cheeseus
剛試過'Math.round(new Date(startDate.value).getTime()/ 1000);'too - nothing。我認爲問題在於'oninput'事件沒有因爲我使用日曆選擇器更改日期,但不知道如何解決此問題。 – cheeseus