我想從輸入中移除所需的屬性。一般的想法是我有一個設置爲必需的字段,該字段具有模式屬性的自定義驗證。當用戶點擊一個按鈕時,我試圖刪除必填字段。HTML5必需的輸入,移除和添加上飛不工作
我在這裏把一個小提琴: https://jsfiddle.net/paulmatos/t1p1wub3/
HTML:
<form>
<input type="text" oninvalid='this.setCustomValidity("Enter a number in the Specified Range");' oninput="try{setCustomValidity('')}catch(e){}" pattern="[0-9]" required="required" name="password" id="password" />
<input type="text" required="required" name="temp" />
<input type="submit" class="btn btn-primary form-control" value="Submit" />
<div class="btn btn-default removeReq">Remove Required</div>
</form>
的Jquery:
$('.removeReq').click(function() {
$('#password').removeAttr('required');
});
我遇到的問題與提交的順序做。
如果您單擊刪除需要,然後提交表單,您會看到它按預期工作。 但是,如果您按相反順序執行這些步驟,請先點擊提交,然後再移除並重試,然後再次提交,您會注意到我仍然在第一個輸入中收到驗證錯誤。
有沒有辦法繞過這與這個預期的功能,我試圖得到這個只與html5驗證工作。
完美地工作。感謝您的幫助。 –