【发布时间】:2020-02-04 13:16:42
【问题描述】:
我的required 属性未指定在提交表单之前必须填写输入字段。
HTML:
<!-- Modal Content -->
<form class="modal-content2">
<div class="container3">
<h1>Sign Up</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<label for="firstName"><b>First Name</b></label>
<input type="text" id="firstName" placeholder="Enter First Name" name="firstName" required>
<label for="lastName"><b>Last Name</b></label>
<input type="text" id="lastName" placeholder="Enter Last Name" name="lastName" required>
<label for="username"><b>Username</b></label>
<input type="text" id="username" placeholder="Enter Username" name="username" required>
<label for="email"><b>Email</b></label>
<input type="text" id="email" placeholder="Enter Email" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" id="password" placeholder="Enter Password" name="psw" onfocus="this.value=''"
required>
<label for="psw-confirm"><b>Confirm Password</b></label>
<input type="password" id="cfmpassword" placeholder="Confirm Password" name="psw-confirm" onfocus="this.value=''"
required>
<br>
<br>
<p>By creating an account you agree to our <a href="aboutus.html" style="color:dodgerblue">Terms &
Privacy</a>.</p>
<div class="clearfix">
<button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn2">Cancel</button>
<button type="button" class="signupbtn" onclick="signUp()">Sign Up</button>
</div>
</div>
</form>
JavaScript:
function signUp() {
if (document.getElementById("password").value == document.getElementById("cfmpassword").value) {
var users = new Object();
users.firstName = document.getElementById("firstName").value;
users.lastName = document.getElementById("lastName").value;
users.username = document.getElementById("username").value;
users.email = document.getElementById("email").value;
users.password = document.getElementById("password").value;
var postUser = new XMLHttpRequest(); // new HttpRequest instance to send user details
postUser.open("POST", "/users", true);
postUser.setRequestHeader("Content-Type", "application/json");
postUser.send(JSON.stringify(users));
//go to the logged in page
window.location = "main.html";
}
else {
alert("Password column and Confirm Password column doesn't match!")
}
}
由于required属性不起作用,用户可以连续提交空表单,这些表单将存储在我的SQL数据库中
我的表单中没有<button type="submit">,因为这使我无法使用windows.location。
我是编程新手,有人可以就如何解决这个问题提供一些建议(带有解释)吗?任何帮助,将不胜感激!非常感谢! (我为此使用香草 JavaScript)
【问题讨论】:
-
你真的应该把代码放在问题里......
-
好吧 onclick 不支持 html5 验证。
-
表单中的
buttons太多 -
你从不使用 css 吗?
-
如果你删除了这 3 行,就会出现 JavaScript 错误!如果你删除了这 3 行,就会出现 JavaScript 错误!为什么?因为您仍然在 Ajax 调用中使用
credentials,这将引发错误。所以电话不会发生,提交将运行.... 像我在回答中建议的那样附上你的代码
标签: javascript html css ajax required