使用jQuery使用自定义主题,正文等进行Mailto

时间:2018-06-21 11:47:01

标签: javascript jquery html mailto

我正在为我的团队创建自定义HTML页面。我们正在使用下拉菜单,文本字段和单选按钮来生成电子邮件的字段。我们都使用IE和Outlook,所以这不是问题。我无法使用“生成电子邮件”按钮来填写邮件。我可以弹出电子邮件窗口,但是所有字段均为空白。我需要根据他们在页面上选择的选项来填写subjectto字段,CC字段和body。这是我的代码:

<script>
    $(function generateEmail() {
        var emailTo = $("#teamName").val();
        var emailCC = $("#CC").val();
        var emailSubject = "Escalation Request - Ticket #: " + $("#ticketNumber").val();
        var emailBody = "Issue: " + $("#issue") + "<br>Contact info: " + $("#contactInformation") + "<br>Requested action: " + $(".requestedAction:checked");
        window.location.href = "mailto:" + emailTo + "&CC=" + emailCC + "&subject=" + emailSubject + "&body=" + emailBody;
    });
</script>

<body>
    <h1>Team</h1>
    <select id="teamName">
        <option value="a@a.com">Team A</option>
        <option value="b@b.com">Team B</option>
        <option value="c@c.com">Team C</option>
    </select><br><br>
    <h1>CC</h1>
    <input type="text" id="CC"><br><br>
    <h1>Issue</h1>
    <input type="text" id="issue"><br><br>
    <h1>Ticket Number</h1>
    <input type="text" id="ticketNumber"><br><br>
    <h1>Customer contact info</h1>
    <input type="text" id="contactInformation"><br><br>
    <h1>Requested action</h1>
    <input type="radio" name="requestedAction" class="requestedAction" value="Expedite service" id="reqActExpediteService" checked>Expedite service<br>
    <input type="radio" name="requestedAction" class="requestedAction" value="Callback" id="reqActCallback">Callback<br><br>
    <input type="submit" value="Generate email" onclick="generateEmail()">
</body>

此外,我还需要使用换行符和粗体字母设置正文格式。上面的代码不起作用。我敢肯定,由于小于/大于符号,因此无法正常工作,但我不知道如何在HTML代码中添加其他内容。我知道这是有可能的,因为我要替换的旧工具能够实现它,我只是不知道该怎么做。放轻松吧,我是jQuery和Java的新手。预先感谢!

2 个答案:

答案 0 :(得分:1)

您在?网址中缺少mailto,因此不会传递查询字符串参数(请注意?之前的cc=):

window.location.href = "mailto:" + emailTo + "?cc=" + emailCC + "&subject=" + emailSubject + "&body=" + emailBody;

要添加换行符,可以使用%0A%0A作为换行符。这样会生成不同的段落,如下所示:

&body=first line %0A%0A second line

您的代码中还存在一些错误,缺少一些val()调用,以获取字段值,并且缺少条件(条件)以检查是否设置了字段(以构建包含这些值的查询字符串)。 / p>

function generateEmail() {
  var emailTo = $("#teamName").val();
  var emailCC = $("#CC").val();
  var emailSubject = "Escalation Request - Ticket #: " + $("#ticketNumber").val();
  var emailBody = "Issue: " + $("#issue").val() + "%0A%0AContact info: " + $("#contactInformation").val() + "%0A%0ARequested action: " + $(".requestedAction:checked").val();
  location.href = "mailto:" + emailTo + "?" + 
    (emailCC ? "cc=" + emailCC : "") + 
    (emailSubject ? "&subject=" + emailSubject : "") + 
    (emailBody ? "&body=" + emailBody : "");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Team</h1>
<select id="teamName">
	<option value="a@a.com">Team A</option>
	<option value="b@b.com">Team B</option>
	<option value="c@c.com">Team C</option>
</select><br><br>
<h1>CC</h1>
<input type="text" id="CC"><br><br>
<h1>Issue</h1>
<input type="text" id="issue"><br><br>
<h1>Ticket Number</h1>
<input type="text" id="ticketNumber"><br><br>
<h1>Customer contact info</h1>
<input type="text" id="contactInformation"><br><br>
<h1>Requested action</h1>
<input type="radio" name="requestedAction" class="requestedAction" value="Expedite service" id="reqActExpediteService" checked>Expedite service<br>
<input type="radio" name="requestedAction" class="requestedAction" value="Callback" id="reqActCallback">Callback<br><br>
<input type="submit" value="Generate email" onclick="generateEmail()">

希望有帮助。

答案 1 :(得分:0)

function generateEmail() {
  var emailTo = $("#teamName").val();
  var emailCC = $("#CC").val();
  var emailSubject = "Escalation Request - Ticket #: " + $("#ticketNumber").val();
  var emailBody = "Issue: " + $("#issue").val() + "%0A%0AContact info: " + $("#contactInformation").val() + "%0A%0ARequested action: " + $(".requestedAction:checked").val();
  location.href = "mailto:" + emailTo + "?" + 
    (emailCC ? "cc=" + emailCC : "") + 
    (emailSubject ? "&subject=" + emailSubject : "") + 
    (emailBody ? "&body=" + emailBody : "");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Team</h1>
<select id="teamName">
	<option value="a@a.com">Team A</option>
	<option value="b@b.com">Team B</option>
	<option value="c@c.com">Team C</option>
</select><br><br>
<h1>CC</h1>
<input type="text" id="CC"><br><br>
<h1>Issue</h1>
<input type="text" id="issue"><br><br>
<h1>Ticket Number</h1>
<input type="text" id="ticketNumber"><br><br>
<h1>Customer contact info</h1>
<input type="text" id="contactInformation"><br><br>
<h1>Requested action</h1>
<input type="radio" name="requestedAction" class="requestedAction" value="Expedite service" id="reqActExpediteService" checked>Expedite service<br>
<input type="radio" name="requestedAction" class="requestedAction" value="Callback" id="reqActCallback">Callback<br><br>
<input type="submit" value="Generate email" onclick="generateEmail()">