2 回答
TA贡献1877条经验 获得超6个赞
你有一个永远不会结束的循环
for (i = 0;; i++) {
你在这里传递不必要的变量
contactInfo(fName, lName, email, country, subject) {
您错误地分配了事件侦听器。你需要
t.addEventListener('click', contactInfo);
没有()
你的引述是错误的
console.log('Your submission details are:' < br > 'First Name: ' + fName)
我想你想要这个
我使用提交事件
如果用户按取消,我会取消提交事件
function contactInfo(event) { // passing the submit event
let 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;
const text = `Your submission details are:
First Name: ${fName}
Last Name: ${lName}
Email: ${email}
Country: ${country}
Subject: ${subject}
Send this now?`
if (!confirm(text)) event.preventDefault(); // stop submission by cancelling the event passed
}
window.addEventListener("load", function() { // when page loads
document.getElementById("contactForm").addEventListener('submit', contactInfo);
});
<div id="form">
<form id="contactForm">
<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>
TA贡献1854条经验 获得超8个赞
尝试这样的事情:
网页:
<form method="post" id="myForm">
<input type="text" name="firstname" placeholder="Your first name...">
<input type="submit" value="Submit" />
</form>
JS:
document.getElementById('myForm').addEventListener ("submit", function (evt) {
evt.preventDefault();
var formData = new FormData(document.getElementById('myForm'))
console.log('Your submission details are:' < br > 'First Name: ' + formData.get('firstname'))
});
添加回答
举报