我正在做一個多階段的表格,但它不是正常的 我已經寫了很多不同的代碼,但不知道爲什麼它不按我想要的方式工作 已經兩天了但我現在覺得自己很笨 這裏是代碼 HTML:有誰知道爲什麼我的多階段表單不起作用?
<div id="form-container">
<div id="phase-1">
<h3>Phase 01</h3>
<form>
<input id="fname" type="text" placeholder="First name">
<input id="lname" type="text" placeholder="Last name">
<input id="email" type="text" placeholder="Email">
<button id="phase-1-btn">Next</button>
</form>
</div>
<div id="phase-2">
<h3>Phase 02</h3>
<form>
<input id="pass" type="text" placeholder="Password">
<input id="cpass" type="text" placeholder="Confirm Password">
<button id="phase-2-btn">Next</button>
</form>
</div>
<div id="phase-3">
<h2>Thank You for Testing my pen</h2>
</div>
</div>
CSS:
#form-container{
height: 350px;
width: 300px;
margin-top: 80px;
margin-left: auto;
margin-right: auto;
background-color: #95a5a6;
font-family: "Slabo 27px";
position: relative;
box-shadow: 1px 1px 2px,
-1px -1px 2px;
}
#phase-1, #phase-2{
height: 100%;
width: 100%;
border-top: 3px solid #f39c12;
display: block;
}
#phase-1 h3, #phase-2 h3{
height: 10%;
width: 60%;
margin-left: auto;
margin-right: auto;
text-align: center;
font-size: 23px;
color: #fff;
}
#phase-1 form, #phase-2 form{
display: block;
height: 75%;
padding: 0;
padding-top: 15px;
margin: 0;
}
input{
display: block;
width: 80%;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
padding: 10px 20px;
border: none;
border-radius: 5px;
}
button {
display: block;
width: 60%;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
padding: 10px 5px;
background-color: #f39c12;
color: #fff;
font-weight: 600;
border: none;
border-radius: 6px;
}
#phase-2{
display: none;
}
#phase-3{
display: none;
height: 0;
width: 100%;
color: #000;
position: absolute;
top: 0;
left: 0;
background: #f39c12
}
#phase-3 h2{
width: 200px;
height: 60px;
margin-left: auto;
margin-right: auto;
margin-top: 135px;
text-align: center;
}
JS:
var fname, lname, email, pass, cpass;
function id(id) {
return document.getElementById(id);
}
function phase1() {
fname = id("fname").value;
lname = id("lname").value;
email = id("email").value;
if (fname.length > 2 && lname.length > 2 && email.length > 2) {
id("phase-1").style.display = "none";
id("phase-2").style.display = "block";
// end of if
} else {
alert("Please fill the Form");
}
} // end of phase1 function
// add the event to the phase-1-btn
id("phase-1-btn").addEventListener("click", phase1());
/* phase 02 */
function phase2() {
pass = id("pass").value;
cpass = id("cpass").value;
if (pass.length > 2 && cpass.length > 2) {
id("phase-2").style.display = "none";
id("phase-3").style.display = "block";
id("phase-3").style.height = "100%";
// end of if
} else {
alert("Please fill the Form");
}
} // end of phase2 function
id("phase-2-btn").addEventListener("click", phase2());
它的工作,謝謝你,但是什麼問題 –
難以調試的原因是BC有超過1個錯誤。當事情開始失敗時,你會想簡化。你最好的朋友是console.log。 不知道哪一個,但我會給你一個可能是罪魁禍首的清單。 1.與常用關鍵字,DOM節點或元素ID相同的命名變量或函數可能會導致問題。 2.在將節點添加到DOM之前應用監聽器。 3.不太可能的原因,但您使用過時的HTML按鈕引用。 –
謝謝!真的appriciated –