我在网站上有一个电子邮件发送器,人们可以填写所需的输入框,然后按发送,我会收到一封电子邮件。所有输入框中的文本都会添加到电子邮件正文中。该网站还有一种方法可以在某些带有 class="tag"的 div 中生成一些文本(div 看起来像某种标签)。如何将所有带有类标签的 div 中的文本添加到电子邮件中?这是我使用的代码:
JS
$(function() {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
var firstName = name; // For Success/Failure Message
// Check for white space in name for Success/Fail message
if (firstName.indexOf(' ') >= 0) {
firstName = name.split(' ').slice(0, -1).join(' ');
}
$.ajax({
url: "././mail/contact_me.php",
type: "POST",
data: {
name: name,
phone: phone,
email: email,
message: message,
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
$('.tag').remove();
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later or send a email at order@o-p-a-l.eu");
$('#success > .alert-danger').append('</div>');
},
})
},
filter: function() {
return $(this).is(":visible");
},
});
$("a[data-toggle=\"tab\"]").click(function(e) {
e.preventDefault();
$(this).tab("show");
});
});
/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});
PHP
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$products = $_POST['products'];
// Create the email and send the message
$to = 'order@order.eu'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message\n\nProduct ordered: $products";
$headers = "From: order@order.eu\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
HTML
<form name="sentMessage" id="contactForm" novalidate>
<input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<div class="cart" contenteditable="false">
<div class="tag">01A001-Z03-71021<span class="remove">X</span></div>
<div class="tag">02A001-Z03-71021<span class="remove">X</span></div>
<div class="tag">03A001-Z03-71021<span class="remove">X</span></div>
<input type="text" class="newtag" placeholder="Your items" id="" readonly />
</div>
<button id="submitbtn" type="submit" class="btn btn-xl">Send Message</button>
</form>
最佳答案
如果您尝试执行类似的操作,可以使用 getElementsByClassName
函数
// find all elements with the 'tag' class
var tags = document.getElementsByClassName('tag');
var tagText = '';
// loop through each found element and get the innerText property
for (var i=0; i< tags.length; i++){
tagText += tags[i].innerText + ' ';
}
// trim the trailing whitespace
tagText = tagText.trim();
您现在想要将 tagText
添加到您的 message
变量
编辑
我刚刚注意到 tag
div 中的子节点。我想这才是真正的问题:如何在不包含 remove
文本的情况下获取 tag
文本?
您可以使用元素的childNodes
属性
// get all the elements as before:
var tags = document.getElementsByClassName('tag');
var tagText = '';
// loop through each found element and use the childNodes property
for (var i=0; i< tags.length; i++){
tagText += tags[i].childNodes[0].nodeValue + ' ';
}
// trim the trailing whitespace
tagText = tagText.trim();
请注意,childNodes
不返回文本。它们是对象,因此要获取值,必须使用 nodeValue
属性。
这里是 MDN 的链接:childNodes
<小时/>将此代码包含到您的代码中
您需要将此代码添加到您的 SubmitSuccess
方法中:
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
// this is where you're getting your message text
var message = $("textarea#message").val();
// add in the childNodes code
var tags = document.getElementsByClassName('tag');
var tagText = '';
for (var i=0; i< tags.length; i++){
tagText += tags[i].childNodes[0].nodeValue + ' ';
}
// trim the trailing whitespace
tagText = tagText.trim();
// add the message body and the tag text together:
message = message + 'Tags: ' + tagText;
// continue your code as before
....
关于javascript - 从 div 中选择文本并将其包含到电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732639/