我目前正在尝试从表单捕获输入数据并将该数据存储到对象数组中,然后该对象数组可以汇总控制台中捕获的数据。最重要的是,我尝试按照我的作业说明运行该函数,使用传统的 DOM 事件处理程序来存储该数据。
JS 的最终目标是使用 var t 通过 ID 'contactSubButton' 获取提交按钮的元素,并运行函数 contactInfo onsubmit...
但是,无论我尝试什么,我都会在控制台的“t.addEventListener”行中收到此消息......
Uncaught TypeError: Cannot read property 'addEventListener' of null
var t = document.getElementById("contactSubButton");
t.addEventListener('click', contactInfo());
function contactInfo(fName, lName, email, country, subject) {
fName = document.getElementById('fname').value;
lName = document.getElementById('lname').value;
email = document.getElementById('email').value;
country = document.getElementById('country').value;
subject = document.getElementById('subject').value;
for (i = 0;; i++) {
var subInfo = [fName, lName, email, country, subject]
}
console.log('Your submission details are:' < br > 'First Name: ' + fName)
}
<div id="form">
<form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your first name...">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lastname" placeholder="Your last name....">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="Please enter your email...">
<label for="country">Country</label>
<select id="country" name="country">
<option value="usa">United States</option>
<option value="canada">Canada</option>
<option value="mexico">Mexico</option>
</select>
<label for="subject">Subject</label>
<textarea id="subject" name="subject" placeholder="How can we help?"></textarea>
<input type="submit" value="Submit" id="contactSubButton">
<input type="reset" value="Reset Form">
</form>
</div>
慕哥9229398
哔哔one
相关分类