您好,登錄后才能下訂單哦!
這篇文章主要講解了“html5中input的required使用報錯怎么解決”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“html5中input的required使用報錯怎么解決”吧!
問題描述
在form表單提交的時候,有些input標(biāo)簽被隱藏,表單驗證過程中會出現(xiàn)An invalid form control with name='' is not focusable
的錯誤
雖然我遇到的問題是我的input標(biāo)簽根本沒有required屬性,但是在該標(biāo)簽隱藏之前,(我的是使用tab欄切換)我輸入了錯誤的格式,再隱藏,這時候他其實是錯誤的,會被form表單同樣去驗證,但是由于它被隱藏,瀏覽器獲取不到焦點(diǎn)就會報錯。
解決方法
隱藏之前將該input的value值設(shè)置為空即可.我的input上面沒有使用required屬性。
如果input含有display:none和required屬性,也會產(chǎn)生該錯誤
產(chǎn)生原因
Chrome希望專注于需要但仍為空的控件,以便可以彈出消息“請?zhí)顚懘俗侄巍薄5?,如果控件在Chrome想要彈出消息的時候隱藏,即在提交表單時,Chrome無法關(guān)注該控件,因為它是隱藏的,因此表單不會提交。
解決方法如下
1.隱藏時,將required屬性刪除
selector.removeAttribute("required")
2.沒有使用required的話,或許是由于button按鈕,類型未設(shè)置造成。設(shè)置<button type="button">
3.form表單不驗證,即添加novalidate屬性。(不是最終解決辦法)<form novalidate></form>
4.既然是由于使用了display:none造成,同樣的visibility: hidden 也會造成問題,那就不使用。通過可以設(shè)置css樣式opacity: 0;
5.禁用此表單控件。 disabled 這是因為通常如果你隱藏了表單控件,那是因為你不關(guān)心它的價值。所以這個表單控件名稱值對在提交表單時不會被發(fā)送。
$("body").on("submit", ".myForm", function(evt) { // Disable things that we don't want to validate. $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", true); // If HTML5 Validation is available let it run. Otherwise prevent default. if (this.el.checkValidity && !this.el.checkValidity()) { // Re-enable things that we previously disabled. $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false); return true; } evt.preventDefault(); // Re-enable things that we previously disabled. $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false); // Whatever other form processing stuff goes here. });
感謝各位的閱讀,以上就是“html5中input的required使用報錯怎么解決”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對html5中input的required使用報錯怎么解決這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。