h5的input的required使用中遇到的問題

form提交時隱藏input發生的錯誤

問題描述

  • 在form表單提交的時候,有些input標籤被隱藏,表單驗證過程當中會出現An invalid form control with name='' is not focusable 的錯誤css

  • 雖然我遇到的問題是個人input標籤根本沒有required屬性,可是在該標籤隱藏以前,(個人是使用tab欄切換)我輸入了錯誤的格式,再隱藏,這時候他實際上是錯誤的,會被form表單一樣去驗證,可是因爲它被隱藏,瀏覽器獲取不到焦點就會報錯。瀏覽器

解決方法

  • 隱藏以前將該input的value值設置爲空便可.個人input上面沒有使用required屬性。

若是input含有display:nonerequired屬性,也會產生該錯誤

  • 產生緣由

Chrome但願專一於須要但仍爲空的控件,以即可以彈出消息「請填寫此字段」。可是,若是控件在Chrome想要彈出消息的時候隱藏,即在提交表單時,Chrome沒法關注該控件,由於它是隱藏的,所以表單不會提交。ui

  • 解決方法以下
  1. 隱藏時,將required屬性刪除
selector.removeAttribute("required")
  1. 沒有使用required的話,或許是因爲button按鈕,類型未設置形成。設置<button type="button">this

  2. form表單不驗證,即添加novalidate屬性。(不是最終解決辦法)<form novalidate></form>code

  3. 既然是因爲使用了display:none形成,一樣的visibility: hidden 也會形成問題,那就不使用。經過能夠設置css樣式opacity: 0;orm

  4. 禁用此表單控件。 disabled 這是由於一般若是你隱藏了表單控件,那是由於你不關心它的價值。因此這個表單控件名稱值對在提交表單時不會被髮送。
$("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.
});

若有其餘更好方法,歡迎留言!ci

參考

https://stackoverflow.com/questions/22148080/an-invalid-form-control-with-name-is-not-focusablerem

https://stackoverflow.com/questions/30644606/an-invalid-form-control-with-name-is-not-focusable-without-any-required-or-hget

相關文章
相關標籤/搜索