所以我的老师给了我们一个任务。部分原因是,如果在按下按钮时选中了复选框,则会说明该客户是否'是否已订阅时事通讯。她还没有教我们jQuery,所以我不认为她希望我们使用它。有没有办法在没有jQuery的情况下完成它?
这是我老师的正文代码:
<div class="error"></div>
<label for="customerFirst">First Name:</label>
<input type="text" id="customerFirst">
<label for="customerLast">Last Name:</label>
<input type="text" id="customerLast">
<label for="customerEmail">Email:</label>
<input type="text" id="customerEmail">
<label for="customerNewsletter" style="font-weight: normal;">Check here to subscribe to the newsletter.</label>
<input type="checkbox" id="customerNewsletter" checked>
<button id="submit" onclick=Create()>Create Customer</button>
<button id="reset" onclick=Reset()>Reset Form</button>
在剧本中,我写了这个:
var customer = {
first: "",
last: "",
email: "",};
var Replace = function() {
customer.first = document.getElementById('customerFirst').value;
customer.last = document.getElementById('customerLast').value;
customer.email = document.getElementById('customerEmail').value;
}
var Create = function() {
Replace();
document.getElementById('customerList').innerHTML +=(customer.first + " " + customer.last + '\'s email is ' + customer.email +);
document.getElementById('customerFirst').value = "";
document.getElementById('customerLast').value = "";
document.getElementById('customerEmail').value = "";
}
var Reset = function() {
document.getElementById('customerFirst').value = "";
document.getElementById('customerLast').value = "";
document.getElementById('customerEmail').value = "";
document.getElementById('customerList').innerHTML = "";
}
知道如何选中复选框会让我更接近完成我的作业,让我看起来对我的老师不那么无能。谢谢你的帮助。
答案 0 :(得分:0)
您可以使用checked
属性查看元素是否已选中
document.getElementById({id}).checked