我有一個問題清除HTML5 oninvalid輸入邊界無效。如果用戶在未正確填寫表單的情況下單擊提交按鈕,將顯示HTML5 setCustomValidity錯誤,並且默認的紅色輪廓將顯示在任何空白的所需輸入字段周圍......這一切都很好。問題是,如果用戶單擊後退按鈕,然後返回到此窗體或此窗體中窗體的其他部分,窗體仍將顯示所有帶有紅色輪廓的無效窗體元素。如何重置HTML5表單驗證錯誤,當點擊返回按鈕
我想要發生的事情是,如果這種形式的「返回」按鈕被單擊,將重置/清除窗體,以便所有窗體元素都被重置,並且除非窗體再次被重置,否則任何窗體元素都不會顯示紅色輪廓提交併且有空/不正確的表單信息。
這裏是我的代碼片段:
<form class="login-form" aria-label="Login" action="" method="post">
<div class="login-A">
<h4>Are you a client?</h4>
<div>
<button class="lx-button lx-button--small lx-button--primary" id="client-yes" type=button>
Yes
</button>
<button class="lx-button lx-button--small lx-button--primary" id="client-no" type=button>
No
</button>
</div>
</div>
<div class="login-B">
<h4>Existing client information</h4>
<button class="lx-button lx-button--small lx-button--tertiary back" id="olb-back-btn-2" type=button>
Go back
</button>
<p>Enter your online ID and Password</p>
<div>
<div>
<label for="olb-user-id">User ID</label>
<input id="olb-user-id" name="olbuserid" maxlength="20" type="text" required="required" oninvalid="setCustomValidity('User ID is required.')" oninput="setCustomValidity('')">
</div>
<div>
<label for="olb-password">Password</label>
<input id="olb-password" name="olbpassword" maxlength="28" value="" type="password" required="required" oninvalid="setCustomValidity('Password is required.')" oninput="setCustomValidity('')">
</div>
</div>
<button class="lx-button lx-button--small lx-button--primary" id="login-button" type=submit>
Continue
</button>
</div><!-- login-B -->
<div class="login-C">
<h4 class="text">New client</h4>
<button class="lx-button lx-button--small lx-button--tertiary back" id="olb-back-btn-3" type=button>
Go back
</button>
<label for="login-state">Select your state to view your account options.</label>
<div class="login-state-container">
<select id="login-state" name="login-state" required="required" oninvalid="setCustomValidity('State is required.')" oninput="setCustomValidity('')">
<option value="" selected="selected">State</option>
<option value="AL">AL</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="IN">IN</option>
<option value="WV">WV</option>
<option value="DC">Washington, DC</option>
</select>
</div>
</div>
</form>
伊夫搜查了好幾天,試圖找出如何解決這個問題。我曾嘗試:
function clearValidity(){
document.getElementById('olb-user-id').setCustomValidity('');
document.getElementById('olb-password').setCustomValidity('');
document.getElementById('login-state').setCustomValidity('');
}
document.getElementById('olb-back-btn-2').onclick = function() {clearValidity()};
document.getElementById('olb-back-btn-3').onclick = function() {clearValidity()};
也試過:
$('.login-form').trigger("reset");
隨着從帖子很多其他試驗我讀,但似乎沒有任何工作。請幫忙!謝謝!
當我說「後退按鈕」它不是瀏覽器後退按鈕,但在我的表單中的後退按鈕。我沒有保存表單信息,也不想單擊表單後退按鈕。 – moudstar
如果您的表單上有一個後退按鈕,只需使用onclick,然後使用document.getElementById('test')。value =「」;這將清除準備好重新使用的表單 –
這不適用於重置所需的HTML5本機表單域。它不會重置無效或無效的字段上的無效紅色邊框 – moudstar