gpt4 book ai didi

javascript - 如何使用 HTML form.checkValidity()?

转载 作者:行者123 更新时间:2023-11-29 16:05:01 24 4
gpt4 key购买 nike

阅读一个使用 MVC 的网站源代码时,我很难理解其中的一些内容。

View 的代码片段:

function submitForm (action) {
var forms = document.getElementById('form')
forms.action = action
if (forms.checkValidity()) {
forms.submit()
} else {
alert('There is still an empty field')
}
}

如果表单缺少某些输入,我想执行一些代码。

最佳答案

checkValidity() 是一个 HTML5 方法并且

When the checkValidity() method is invoked, if the element is a candidate for constraint validation and does not satisfy its constraints, the user agent must fire a simple event named invalid that is cancelable (but in this case has no default action) at the element and return false. Otherwise, it must only return true without doing anything else.

请详细了解如何使用表单验证约束here .您可以使用、自定义甚至构建自定义验证方法的有用方法有很多。

您还可以在 w3schools 中找到基本解释和示例.希望这会有所帮助。

关于javascript - 如何使用 HTML form.checkValidity()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45789010/

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