Javascript 模态表单应该使用什么事件处理程序发送电子邮件
我正在创建一个web应用程序,我想使用EmailJS()通过表单将消息发送到我的电子邮件。他们有一种简单的方式,当表单加载到页面上时,您可以向自己发送电子邮件Javascript 模态表单应该使用什么事件处理程序发送电子邮件,javascript,html,jquery,dom-events,bootstrap-modal,Javascript,Html,Jquery,Dom Events,Bootstrap Modal,我正在创建一个web应用程序,我想使用EmailJS()通过表单将消息发送到我的电子邮件。他们有一种简单的方式,当表单加载到页面上时,您可以向自己发送电子邮件 <script type="text/javascript"> (function() { // https://dashboard.emailjs.com/admin/integration emailjs.init('YOUR_USER_ID');
<script type="text/javascript">
(function() {
// https://dashboard.emailjs.com/admin/integration
emailjs.init('YOUR_USER_ID');
})();
</script>
<script type="text/javascript">
**window.onload =** function() {
document.getElementById('contact-form').addEventListener('submit', function(event) {
event.preventDefault();
// generate a five digit number for the contact_number variable
this.contact_number.value = Math.random() * 100000 | 0;
// these IDs from the previous steps
emailjs.sendForm('contact_service', 'contact_form', this)
.then(function() {
console.log('SUCCESS!');
}, function(error) {
console.log('FAILED...', error);
});
});
}
</script>
(功能(){
// https://dashboard.emailjs.com/admin/integration
emailjs.init('YOUR_USER_ID');
})();
**window.onload=**函数(){
document.getElementById('contact-form')。addEventListener('submit',函数(事件){
event.preventDefault();
//为contact_number变量生成一个五位数的数字
this.contact_number.value=Math.random()*100000 | 0;
//这些ID来自前面的步骤
emailjs.sendForm('contact\u service','contact\u form',this)
.然后(函数(){
console.log('SUCCESS!');
},函数(错误){
console.log('FAILED…',error);
});
});
}
在我的例子中,我使用的是带引导的模式表单,在他们的案例教程中,他们只包含了一种情况,当表单已经加载到屏幕上时,windows.onload。对于已经加载的表单(在屏幕上的表单),它可以正常工作。我的问题是我应该使用什么处理程序,如果我要使用模态表单,因为它是在onload已经发生之后加载的
下面是我的屏幕代码片段
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Form Testing</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"> </script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script>
<script type="text/javascript">
(function () {
emailjs.init("user_p07SKunpARYp4aowW97yr");
})();
</script>
</script>
<script type="text/javascript">
window.onload = function () {
document.getElementById('modal-form').addEventListener('submit', function (event) {
event.preventDefault();
// generate a five digit number for the contact_number variable
this.contact_number.value = Math.random() * 100000 | 0;
// these IDs from the previous steps
emailjs.sendForm('service_k34ck3j', 'contact_form', this)
.then(function () {
console.log('SUCCESS!');
}, function (error) {
console.log('FAILED...', error);
});
});
}
</script>
</head>
<body>
<form class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="modal-dialogLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modal-dialogLabel">User Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- actual form markup -->
<div class="form-group">
<input name="name" type="text" class="form-control" id="field1" placeholder="name">
</div>
<div class="form-group">
<input name="user_email" type="email" class="form-control" id="field2" placeholder="email">
</div>
<div class="form-group">
<input name="user_number" type="number" class="form-control" id="field2" placeholder="number">
</div>
<div class="form-group">
<textarea type="text" id="form8" class="md-textarea form-control" rows="4" name="message"
placeholder="Example : how much does it cost to roof a 10*20 house using harvey tiles"></textarea>
</div>
<!-- /actual form markup -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button id="save" data-dismiss="modal" type="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</form>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-form">
Open modal form
</
button>
</body>
</html>
形式测试
(功能(){
emailjs.init(“用户_p07SKunpARYp4aowW97yr”);
})();
window.onload=函数(){
document.getElementById('modal-form')。addEventListener('submit',函数(事件){
event.preventDefault();
//为contact_number变量生成一个五位数的数字
this.contact_number.value=Math.random()*100000 | 0;
//这些ID来自前面的步骤
emailjs.sendForm('service_k34ck3j','contact_form',this)
.然后(函数(){
console.log('SUCCESS!');
},函数(错误){
console.log('FAILED…',error);
});
});
}
用户详细信息
&时代;
接近
保存更改
开放模态形式
由于您使用引导,因此可以使用引导模式事件-https://getbootstrap.com/docs/4.0/components/modal/#events
对于模式加载后的触发功能,您可以使用事件“Showed.bs.modal”
$("#modal-form").on('shown.bs.modal', function(){
alert('The modal is fully shown.');
});
我假设
**window.onload=**function(){
是试图将代码加粗,而不是那些*
实际存在于您的代码中?是的,这当然是一个错误,但我相信问题是清楚的