[英]Using smtpjs to send the checkbox value of the form in pure javascript
I to use SmtpJS to capture the value of my form and send it to my email.我使用 SmtpJS 来捕获我的表单的值并将其发送到我的 email。
So far I can get the input value without any problems到目前为止,我可以毫无问题地获得输入值
but how do I get the value of the checked checkbox?但是如何获取选中复选框的值呢?
<div class="form-group">
<label class="contact_us_title">Name</label>
<input type="text" class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group">
<label class="contact_us_title">Question</label>
<div >
<label class="checkbox-inline"><input type="checkbox" value="">A</label>
<label class="checkbox-inline"><input type="checkbox" value="">B</label>
<label class="checkbox-inline"><input type="checkbox" value="">C</label>
</div>
</div>
<script>
function sendMail() {
let fields = {
name: document.querySelector("#name").value,
};
let body = 'Name:' + fields.name;
Email.send({
Host : "smtp.yourisp.com",
Username : "username",
Password : "password",
To : 'them@website.com',
From : "you@isp.com",
Subject: "title",
Body: body,
}).then(
message => alert(message)
);
}
</script>
Added submit button to call sendMail function and added class and update value of checkbox to get values!添加提交按钮以调用 sendMail function 并添加 class 并更新复选框的值以获取值!
<div class="form-group">
<label class="contact_us_title">Name</label>
<input type="text" class="form-control" id="name" placeholder="Name" />
</div>
<div class="form-group">
<label class="contact_us_title">Question</label>
<div>
<label class="checkbox-inline"
><input type="checkbox" class="ques" value="A" />A</label
>
<label class="checkbox-inline"
><input type="checkbox" class="ques" value="B" />B</label
>
<label class="checkbox-inline"
><input type="checkbox" class="ques" value="C" />C</label
>
</div>
</div>
<input type="submit" onclick="sendMail()" />
<script>
function sendMail() {
const questionValues = [...document.querySelectorAll('.ques:checked')].map(
(e) => e.value
);
let fields = {
name: document.querySelector('#name').value,
question: questionValues,
};
console.log(fields);
let body = 'Name:' + fields.name;
Email.send({
Host : "smtp.yourisp.com",
Username : "username",
Password : "password",
To : 'them@website.com',
From : "you@isp.com",
Subject: "title",
Body: body,
}).then(
message => alert(message)
);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.