javascript - 表单验证功能在提交时不运行
问题描述
我有一个用于验证表单值的函数。我已将它添加到onsubmit
属性中,但它不会在它应该运行的时候运行。我把它放在正文中,在提交按钮之前,但页面一开始就运行该功能。
我想checkForm
在最后运行以验证数据的输入,但是每当我提交表单时,页面就会刷新。
我的函数是在错误的地方定义或调用的吗?我需要进行哪些更改才能停止页面刷新并让我的函数验证表单数据?
<div id="mc_embed_signup">
<form id="form" method="post" onsubmit="return checkForm(this)">
<script>
function checkForm(form)
if(radioValue = checkRadio(form.radiofield)) {
return true;
else {
alert("Seleccione un tipo de operación");
return false;
}
return true;
</script>
解决方案
提交时可以使用preventDefault()
JS防止刷新
有关更多详细信息-> 转到此处:https ://www.w3schools.com/jsref/event_preventdefault.asp
推荐阅读
- c - 如何使用 stm32f100 板读写 I2C eeprom
- r - 了解 lapply 以获取数据框列表
- c# - ASP.Net WEB API 无法 POST 来自移动设备的大型 JSON 数据
- clickhouse - 启用监听主机 0.0.0.0 后 clickhouse-client 无法登录
- python - 将嵌套数组展平为行
- url - 如何在电子表格中获取可编辑表单响应的 URL
- javascript - 如何使用 For 循环通过递增的 ID 来定位输入字段
- docker - Jenkins 错误安装 Xvfb 并再次运行 Cypress
- amazon-web-services - 在 x86 桌面上运行使用 ARMv7 基础映像构建的 Docker 映像时出现问题
- angular - PrimeNG 的 TableEdit 中的 EditMode 不起作用