gpt4 book ai didi

javascript - 如何使标签可见/不可见?

转载 作者:技术小花猫 更新时间:2023-10-29 12:12:39 31 4
gpt4 key购买 nike

我有这些日期和时间字段,我想为时间设置一个 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>

问题是无论我将什么设置为可见,标签都会在加载时显示。我试过 visibility = "hidden"但它仍然显示。

这是验证部分:

    <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>

那么,我应该怎么做呢? Google 显示了不同的验证方法,但没有显示如何隐藏/显示标签

最佳答案

您正在寻找显示:

document.getElementById("endTimeLabel").style.display = 'none';
document.getElementById("endTimeLabel").style.display = 'block';

编辑:您还可以轻松地重用您的验证函数。

HTML:

<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, 'startTimeLabel');"/>
<label id="startTimeLabel" class="errorMsg">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="validateHHMM(this.value, 'endTimeLabel');"/>
<label id="endTimeLabel" class="errorMsg">Time must be entered in the format HH:MM AM/PM</label>

Javascript:

function validateHHMM(value, message) {
var isValid = /^(0?[1-9]|1[012])(:[0-5]\d) [APap][mM]$/.test(value);

if (isValid) {
document.getElementById(message).style.display = "none";
}else {
document.getElementById(message).style.display= "inline";
}

return isValid;
}

Live DEMO

关于javascript - 如何使标签可见/不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10857393/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com