gpt4 book ai didi

javascript - window.onload方法,如何调用多个函数

转载 作者:行者123 更新时间:2023-12-03 09:35:17 27 4
gpt4 key购买 nike

编写一些 JavaScript,以便表单在字段已填写完毕后不会提交,如果用户尚未填写,则会弹出一条消息,提示“请填写姓名字段”或“请填写电子邮件字段”我正在为每个字段编写一个单独的函数(不知道这是否是正确的方法),当我将每个函数添加到 window.onload 时,它只会弹出一条消息。任何建议都会很棒。

HTML

 <form id="frmContact" name="frmContact" method="post" action="thanks.htm">
<fieldset id="quickSupport">
<legend><strong>Quick Support</strong></legend>
<p> If your request isn't urgent, drop us a quick line and we'll get back to you within 24 hours </p>
<p>
<label for="name">Name:</label>
<input type="text" value="Your Name" name="name" id="name" tabindex="10" />
</p>
<p>
<label for="email">Email:</label>
<input type="text" value="Your Email" name="email" id="email" tabindex="20" />
</p>
<label for="comments">Comments </label>
<br />
<textarea name="comments" value="Message" id="comments" cols="45" rows="5" tabindex="60" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">Message</textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Send" tabindex="70" />
</p>
</fieldset>
</form>

<p><span id="errorMessage"></span></p>
<p><span id="errorMessage1"></span></p>

JavaScript

function prepareEventHandlers () {
document.getElementById("frmContact").addEventListener("submit", function(event) {

// Show message
if (document.getElementById("email").value == "Your Email") {
document.getElementById("errorMessage").innerHTML = "Please provide at least an email address!";
// to STOP the form from submitting
return false;
} else {
// reset and allow the form to submit
document.getElementById("errorMessage").innerHTML = "";
return true;
}

event.preventDefault(); // Prevent form from submitting
}
});
}

function prepareEventHandlersName () {
document.getElementById("frmContact").addEventListener("submit", function(event) {

// Show message
if (document.getElementById("name").value == "Your Name") {
document.getElementById("errorMessage1").innerHTML = "Please provide a name!";
// to STOP the form from submitting
return false;
} else {
// reset and allow the form to submit
document.getElementById("errorMessage1").innerHTML = "";
return true;
}

event.preventDefault(); // Prevent form from submitting
}
});
}


function start() {
prepareEventHandlers();
prepareEventHandlersName();
}

window.onload = 开始;

最佳答案

您应该使用浏览器的内置验证属性。

<form id="frmContact" name="frmContact" method="post" action="thanks.htm">
<fieldset id="quickSupport">
<legend><strong>Quick Support</strong></legend>
<p>If your request isn't urgent, drop us a quick line and we'll
get back to you within 24 hours</p>
<p>
<label for="name">Name:</label>
<input type="text" value="Your Name" name="name" id="name"
tabindex="10" <em><strong>required</strong></em> />
</p>
<p>
<label for="email">Email:</label>
<input type="text" value="Your Email" name="email" id="email"
tabindex="20" <em><strong>required</strong></em> />
</p>
<label for="comments">Comments</label>
<br />
<textarea name="comments" value="Message" id="comments" cols="45"
rows="5" tabindex="60"
<em><strong>placeholder="PLACEHOLDER WHEN NO MESSAGE IS WRITTEN" required</strong></em>>Message</textarea>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Send"
tabindex="70" />
</p>
</fieldset>
</form>

当您尝试提交表单时,浏览器会自动以用户的语言为您显示友好的错误消息。无需 JavaScript。

<小时/>

您的代码失败的原因是,因为

document.getElementById("frmContact").onsubmit = ...

将覆盖您之前添加的任何 onsubmit 处理程序。

您需要使用.addEventListener() 相反,如果您想添加多个功能。

更正确的方法是创建一个执行所有检查的函数,并将其附加为处理程序一次。

关于javascript - window.onload方法,如何调用多个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com