表單填寫樣本(通過JavaScript訪問DOM)

來源:互聯網
上載者:User

標籤:via   val   技術   content   log   secret   input   script   http   

自己寫就的代碼,再爛也是自己的…

實現代碼

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VisitDOMviaJavaScript</title>
<style>
.form {
border: 1px #5e5e5e solid;
width: 40%;
margin: 10px auto;
}

.divTitle {
background-color: #9d9d9d;
padding: 5px;
}

.content {
padding: 8px;
font-size: 10pt;
}

input {
margin: 5px auto;
}

input[type="button"] {
margin: 8px auto;

}

#showTip {
margin: 10px auto;
display: none;
}
</style>
</head>
<body>
<div class="form">
<div class="divTitle">請輸入如下資訊</div>
<div class="content">
姓名:<input type="text" placeholder="請輸入你的姓名" id="name"><br/>
性別:<input type="radio" id="genderRadio1" name="gender">男
<input type="radio" id="genderRadio2" name="gender">女<br/>
婚姻狀況:<input type="checkbox" value="Married" id="CheckboxMerry1">已婚
<input type="checkbox" value="Secret" id="CheckboxMerry2">幫我保密<br/>
<input type="button" value="填寫完畢並提交" onclick="btnClick()">
</div>
</div>
<div class="form" id="showTip"></div>
<script>
function btnClick() {
var inputName = document.getElementById(‘name‘).value;
/*姓名欄框為必填的項目*/
if (inputName == "") {
alert("請填寫姓名");
} else {
var inputGender = (genderRadio1.checked) ? "男" : "女";
/*當genderRadio1選中時,性別為男,當genderRadio1沒有被勾選時候,性別為女*/
var inputMerry = (CheckboxMerry1.checked && CheckboxMerry2.checked) ? "已婚且幫我保密" : "你猜去!";
document.getElementById(‘showTip‘).style.display = "block";
document.getElementById(‘showTip‘).innerHTML = "<b>你填寫的資訊如下:</b>" + "<br/>" + inputName + "&nbsp;" + inputGender + "&nbsp;" + inputMerry;
}
}
</script>
</body>
</html>

頁面效果

表單填寫樣本(通過JavaScript訪問DOM)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.