首页 > 解决方案 > 当我在 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>

标签: javascripthtmleclipse

解决方案


尝试这个:

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 并在表单得到验证时将其插入。


推荐阅读