使用smtpjs纯发送表单的checkbox值 javascript
Using smtpjs to send the checkbox value of the form in pure javascript
我要使用 SmtpJS 来捕获我的表单的值并将其发送到我的电子邮件。
到目前为止,我可以毫无问题地获取输入值
但是如何获取选中复选框的值?
<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>
添加了提交按钮以调用 sendMail 函数并添加了 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>
我要使用 SmtpJS 来捕获我的表单的值并将其发送到我的电子邮件。
到目前为止,我可以毫无问题地获取输入值
但是如何获取选中复选框的值?
<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>
添加了提交按钮以调用 sendMail 函数并添加了 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>