我目前正在学习JS,并试图创建一个验证函数,以查看字段是否为空。当我点击提交按钮时,我希望在Chrome的JS控制台中看到一些东西,但我什么都看不到。为什么这不会在控制台中回显任何内容?表单onclick不工作
var field1 = document.getElementById("field1");
var field2 = document.getElementById("field2");
var submitBtn = document.getElementById("submitBtn");
function validate() {
if(field1 === "" || field2 === "") {
console.log("Make sure all fields have been filled out correctly");
} else {
console.log("they are full");
}
}
<form action="" action="get">
<input type="text" id="field1" name="field1" autocomplete="off" placeholder="Enter something"/>
<input type="text" id="field2" name="field2" autocomplete="off" placeholder="Enter that thing again"/>
<input type="submit" id="submitBtn" name="submitBtn" value="Submit" onclick="return validate()" />
</form>
你需要确保你的JS来到你的HTML后。所有的JS应该直接在