如何创建检查输入但不提交数据的HTML按钮?

时间:2018-06-13 11:04:11

标签: html forms button

我有一个用户可以登录的HTML页面。要验证输入是否有效,请使用一些按钮标记属性。 e.g:

required=""
type="email"
pattern="[a-zA-Z0-9_]{1,20}"

在表单底部是一个按钮:<button onclick="submitData()"></button>。如果用户点击它,输入会发生其他一些事情。

现在,如果我使用type="button",则会执行函数submitData(),但不会检查输入。

但如果我使用type="submit",则会检查输入是否符合某些条件,并立即重新加载页面,因此我无法对输入执行任何操作。

那么,如何创建一个检查输入但不提交数据的按钮?

提前谢谢你,

莱昂

示例代码段:

&#13;
&#13;
function submitData() {
  //more stuff...
}
&#13;
h1 {
  width: 100%;
  font-size: 18px;
  background: #18aa8d;
  color: white;
  line-height: 150%;
  border-radius: 3px 3px 0 0;
  box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.2);
}

form {
  font-family: Roboto, sans-serif;
  box-sizing: border-box;
  width: 260px;
  margin: auto;
  box-shadow: 2px 2px 5px 1px rgba(0, 0, 0, 0.2);
  padding-bottom: 40px;
  border-radius: 3px;
}
form h1 {
  box-sizing: border-box;
  padding: 20px;
}

input {
  margin: 40px 25px;
  width: 200px;
  display: block;
  border: none;
  padding: 10px 0;
  border-bottom: solid 1px #1abc9c;
  -webkit-transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
          transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  background: -webkit-linear-gradient(top, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);
  background-position: -200px 0;
  background-size: 200px 100%;
  background-repeat: no-repeat;
  color: #0e6252;
}
input:focus, input:valid {
  box-shadow: none;
  outline: none;
  background-position: 0 0;
}
input:focus::-webkit-input-placeholder, input:valid::-webkit-input-placeholder {
  color: #1abc9c;
  font-size: 11px;
  -webkit-transform: translateY(-20px);
          transform: translateY(-20px);
  visibility: visible !important;
}

button {
  border: none;
  background: #1abc9c;
  cursor: pointer;
  border-radius: 3px;
  padding: 6px;
  width: 200px;
  color: white;
  margin-left: 25px;
  box-shadow: 0 3px 6px 0 rgba(0, 0, 0, 0.2);
}
button:hover {
  -webkit-transform: translateY(-3px);
      -ms-transform: translateY(-3px);
          transform: translateY(-3px);
  box-shadow: 0 6px 6px 0 rgba(0, 0, 0, 0.2);
}

input:focus::-webkit-input-placeholder, input:valid::-webkit-input-placeholder {
  color: #1abc9c;
  font-size: 11px;
  -webkit-transform: translateY(-20px);
          transform: translateY(-20px);
  visibility: visible !important;
}
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Register</title>
</head>
<body>
  <form>
    <h1>Register</h1>
    <input placeholder="Accountname"
           name="accountname"
           type="text"
           required=""
           autocomplete="off"
           pattern="[a-zA-Z0-9_]{1,20}"
           title="'a-z', 'A-Z', '0-9', '_'">
    <input placeholder="Password"
           name="password"
           type="password"
           required=""
           pattern="[a-zA-Z0-9_]{8,}"
           title="'a-z', 'A-Z', '0-9', '_'">
    <button type="submit" onclick="submitData()">Submit</button>
  </form>
</body>
</html>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

试试这个

<button type="submit" onclick="submitData(); return false">Submit</button>

答案 1 :(得分:0)

使用return false来阻止表单提交

function submitData() {
  return false;
}

答案 2 :(得分:0)

在你的submitData()函数中使用return false;它会停止提交表单。

答案 3 :(得分:0)

试试这个

function submitData() {
  //more stuff...
alert("hello world");
}
h1 {
  width: 100%;
  font-size: 18px;
  background: #18aa8d;
  color: white;
  line-height: 150%;
  border-radius: 3px 3px 0 0;
  box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.2);
}

form {
  font-family: Roboto, sans-serif;
  box-sizing: border-box;
  width: 260px;
  margin: auto;
  box-shadow: 2px 2px 5px 1px rgba(0, 0, 0, 0.2);
  padding-bottom: 40px;
  border-radius: 3px;
}
form h1 {
  box-sizing: border-box;
  padding: 20px;
}

input {
  margin: 40px 25px;
  width: 200px;
  display: block;
  border: none;
  padding: 10px 0;
  border-bottom: solid 1px #1abc9c;
  -webkit-transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
          transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  background: -webkit-linear-gradient(top, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);
  background-position: -200px 0;
  background-size: 200px 100%;
  background-repeat: no-repeat;
  color: #0e6252;
}
input:focus, input:valid {
  box-shadow: none;
  outline: none;
  background-position: 0 0;
}
input:focus::-webkit-input-placeholder, input:valid::-webkit-input-placeholder {
  color: #1abc9c;
  font-size: 11px;
  -webkit-transform: translateY(-20px);
          transform: translateY(-20px);
  visibility: visible !important;
}

button {
  border: none;
  background: #1abc9c;
  cursor: pointer;
  border-radius: 3px;
  padding: 6px;
  width: 200px;
  color: white;
  margin-left: 25px;
  box-shadow: 0 3px 6px 0 rgba(0, 0, 0, 0.2);
}
button:hover {
  -webkit-transform: translateY(-3px);
      -ms-transform: translateY(-3px);
          transform: translateY(-3px);
  box-shadow: 0 6px 6px 0 rgba(0, 0, 0, 0.2);
}

input:focus::-webkit-input-placeholder, input:valid::-webkit-input-placeholder {
  color: #1abc9c;
  font-size: 11px;
  -webkit-transform: translateY(-20px);
          transform: translateY(-20px);
  visibility: visible !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Register</title>
</head>
<body>
  <form>
    <h1>Register</h1>
    <input placeholder="Accountname"
           name="accountname"
           type="text"
           required=""
           autocomplete="off"
           pattern="[a-zA-Z0-9_]{1,20}"
           title="'a-z', 'A-Z', '0-9', '_'">
    <input placeholder="Password"
           name="password"
           type="password"
           required=""
           pattern="[a-zA-Z0-9_]{8,}"
           title="'a-z', 'A-Z', '0-9', '_'">
    <button type="buttonn" onclick="submitData()">Submit</button>
  </form>
</body>
</html>