所以我試圖重置一個日期,如果輸入的日期是今天或過去,只是想知道為什么date = ""不作業,因為我已經在前面宣告了變數 "date",但我發現如果我輸入document.getElementById("date").value = "";反而可以作業,當(我推測)他們是同樣的事情?
function validate_date() {
//轉換為Date物件進行比較。
var date = document.getElementById("date"/span>).value。
var date_object = new Date(date)。
var today = new Date() 。
if (date_object <= today) {
alert("Date cannot be today or the past!") 。
//document.getElementById("date").value = "";
date = ""; //doesn't reset。
}
}
< input type="date" name="date" id="date" onblur="validate_date()。 ">
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
當直接選擇一個DOM元素的屬性時,該變數成為一個獨立物體。
通過選擇元素而允許訪問改變它的屬性。
。function validate_date() {
var date = document.getElementById("date"/span>)。
date.value = ""。
}
< input type="date" name="date" id="date" onblur="validate_date()。 ">
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
如果你需要使用行內JS,將事件傳遞給函式,然后從中獲取值。然后你可以重置這個值。這樣你就不需要輸入的id了。
。function validate_date(e) {
var date = e.value。
var date_object = new Date(date)。
var today = new Date() 。
if (date_object <= today) {
alert("Date cannot be today or the past!") 。
e.value = ''/span>;
}
}
< input type="date" onblur="validate_date(this); ">
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331327.html
標籤:
上一篇:給定相機引數,我如何找到從視圖空間到像素坐標的轉換?我的矩陣有什么問題?
下一篇:自定義404頁面重定向的無限回圈
