我有這些日期和時間字段,我想設置一個JavaScript驗證的時間。如何使標籤可見/不可見?
如果格式無效,它應該使標籤可見,否則它應該是不可見的。
這是我到目前爲止的代碼。
<td nowrap="nowrap" align="left">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="right" nowrap="nowrap">
<span id="startDateLabel">Start date/time: </span>
<input id="startDateStr" name="startDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
<button id="startDateCalendarTrigger">...</button>
<input id="startDateTime" type="text" size="8" name="startTime" value="12:00 AM" onchange="validateHHMM(this.value);"/>
<label id="startTimeLabel" visible="false">Time must be entered in the format HH:MM AM/PM</label>
<BR>
<span id="endDateLabel">End date/time: </span>
<input id="endDateStr" name="endDateStr" size="8" onchange="if (!formatDate(this,'USA')) {this.value = '';}" />
<button id="endDateCalendarTrigger">...</button>
<input id="endDateTime" type="text" size="8" name="endTime" value="12:00 AM" onchange="validateHHMM2(this.value);"/>
<label id="endTimeLabel" visible="false">Time must be entered in the format HH:MM AM/PM</label>
</td>
</tr>
</table>
</td>
問題是標籤出現時加載不管我設置爲可見。我試着知道=「隱藏」,它仍然顯示出來。
這裏是驗證部分:
<script>
function validateHHMM(inputField) {
var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(inputField.value);
if (isValid) {
document.getElementById("startTimeLabel").style.visibility = "hidden";
}else {
document.getElementById("startTimeLabel").style.visibility = "visible";
}
return isValid;
}
function validateHHMM2(inputField) {
var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(inputField.value);
if (isValid) {
document.getElementById("endTimeLabel").style.visibility = "hidden";
}else {
document.getElementById("endTimeLabel").style.visibility = "visible";
}
return isValid;
}
</script>
那麼,應該怎麼去呢?谷歌表示了不同的驗證方法,但不知道如何隱藏/顯示標籤