javascript - 当我在 Eclipse 浏览器中运行 url 时,HTML 表单未验证,它不起作用
问题描述
在提交呼叫之前,我必须进行某种形式的验证。当我在 chrome 中运行 URL 时,以下代码可以正常工作。但是当我在 eclipse 中执行相同的操作时"Run on server"
,表单不会被验证。它带我到本地主机页面。你能帮我解决这个问题吗?提前致谢!
function emptyvalidation(field, errorMessage) {
if (field.value == null || field.value == "") {
alert(errorMessage);
field.focus();
return false;
} else {
return true;
}
}
function validateAndSend() {
if (emptyvalidation(nameField,"Please enter your Name")==false) {nameField.focus(); return false;}
document.getElementById("am_submit").disabled = true;
}
<!DOCTYPE html>
<html>
<div id = "amf" >
<div id = "amTitle" > Enter your form header here </div>
<form onsubmit = "return validateAndSend()" method = "post" target = "_self" name = "amform" id = "amform" action ="http://localhost:8080/api/postForm" autocomplete = "off" >
<div style="margin:6px">
<label style="display:inline-block;width:150px;vertical-align:top">Name:</label>
<input type="text" class="inputBox" id="nameField" name="nameField" value="">
</div>
<div style="margin:6px"></div>
<div class = "ambutton" >
<input type = "submit" class = "am_submit" id = "am_submit" value = "Subscribe" >
</div>
</form>
</div>
</html>
解决方案
尝试这个:
function emptyvalidation(field, errorMessage) {
if (field.value == null || field.value == "") {
alert(errorMessage);
field.focus();
return false;
} else {
return true;
}
}
function validateAndSend() {
if (emptyvalidation(nameField,"Please enter your Name")==false) {
nameField.focus();
return false;
} else {
document.getElementById("amform").action = document.getElementById("amform").getAttribute("data-action");
document.getElementById("am_submit").disabled = true;
}
}
<!DOCTYPE html>
<html>
<div id = "amf" >
<div id = "amTitle" > Enter your form header here </div>
<form onsubmit = "return validateAndSend()" method = "post" target = "_self" name = "amform" id = "amform" data-action ="http://localhost:8080/api/postForm" autocomplete = "off" >
<div style="margin:6px">
<label style="display:inline-block;width:150px;vertical-align:top">Name:</label>
<input type="text" class="inputBox" id="nameField" name="nameField" value="">
</div>
<div style="margin:6px"></div>
<div class = "ambutton" >
<input type = "submit" class = "am_submit" id = "am_submit" value = "Subscribe" >
</div>
</form>
</div>
</html>
我刚刚删除了操作 URL 并在表单得到验证时将其插入。
推荐阅读
- android - 在 MVVM 中,决定导航流程是谁的工作?视图模型还是视图?
- asp.net - 如何在 IIS 上从 ASP.NET 加载 ActiveReport 中的图像
- python - Python - 在同一远程服务器上复制文件的最有效方法
- javascript - 如何仅从 Ajax 获取最新的值响应?
- mysql - 使用mysql在datagridview上显示pdf文件
- javascript - 从另一个发送参数的任务中调用 gulp 任务两次
- ios - 为什么 Xcode 9 默认创建我的出口(强,非原子)
- docker - 将 Kibana 容器与 Elasticsearch 连接起来
- r - 当另一个变量的一个循环内的所有数字等于 NA 时删除行
- c# - .net core 2.0 JWT 令牌