【问题标题】:Populate email with entries from modal form使用模态表单中的条目填充电子邮件
【发布时间】:2019-02-11 21:34:07
【问题描述】:

我有一个模态表单(有关模态表单,请参见 HTML sn-p - 这只是整个页面 HTML 的一小部分),以允许用户向我提交新链接。这是我在共享驱动器上为我的团队维护的本地 HTML 文件,我们无权访问托管此文件的服务,因此当提交新链接时,我只需进入并使用新链接更新 HTML .

我希望能够从 modal-body 的每个字段中获取值来填充电子邮件的正文,每个字段都在自己的行中,如下所示:

  • 姓名:[在姓名字段中输入的值]
  • URL:[在 URL 字段中输入的值]
  • 说明:[在说明字段中输入的值]
  • 类别:[从 NewCat 下拉字段中选择的值]

我还希望主题行填充“提交的新链接”或类似内容。

  var emailTO = "test@example.com"
  var emailSubj = "New Link Submission"
  var newName = document.getElementById("newName").value
  var newURL = document.getElementById("newURL").value
  var newDescrip = document.getElementById("newDescrip").value
  var newCat = document.getElementById("newCat").value
  var emailBody = newName % OA % OA newURL % OA % OA newDescrip % OA % OA newCat

function emailLink(){

/* ?????? */

}
<div id="myModal" class="modal" align="center">
  <div class="modal-content">

    <div class="modal-header">
      <span id="close" class="close">&times;</span>
      <h3>Submit New Link</h3>
    </div>

    <div class="modal-body">
      <p>Name: <input type="text" class="newName" id="newName" value="newName" </p>
        <p>URL: <input type="text" class="newURL" id="newURL" value="newURL" </p>
          <p>Description: <input type="text" class="newDesrcip" id="newDesrcip" value="" </p>
            <form>
              <p>Category:
                <select id="newCat" required>
                  <option value="A"> A </option>
                  <option value="B"> B </option>
                  <option value="C"> C </option>
                  <option value="D"> D </option>
                </select>
              </p>
            </form>
    </div>
    <br>
    <input type="button" id="submit_link_button" value="Submit" onclick="emailLink()">

    <!--
I would like to be able to grab the values from each field of the modal-body populate the body of an email, each on it's own line like this:
Name: [value entered in Name field]
URL: [value entered in URL field]
Description: [value entered in Description field]
Category: [value selected from NewCat dropdown field]

I'd also like the Subject line to populate with "New Link Submitted" or something like that.
-->


  </div>
</div>

【问题讨论】:

  • 您应该使用带有href 属性的a 标签包含您想要的电子邮件。但请记住,当您单击 a 标记时,您不会自动发送电子邮件,您只会提示您机器中的默认电子邮件软件/应用程序(即:Windows 上的 Outlook,Android 上的 Gmail... ) 然后您必须按发送以将该电子邮件发送到在a 标记的mailto URL 中提供的电子邮件。通过添加主题、消息等...当默认消息传递应用程序出现时,您会将这些信息放置在适当的位置。希望您明白使用mailto URL 的目的。
  • 感谢您的回复 - 我无需在电子邮件上单击发送就可以了 - 我更纠结于如何使用输入到模式中的四条信息来填充电子邮件的正文表单(名称、URL、描述和类别)
  • 使用JavaScript!你有没有尝试过?
  • 我是 JS 新手,但我已经定义了我的变量(我更新了上面的代码 sn-p)——只是不确定从那里去哪里让 JS 构建电子邮件。你知道怎么做吗?
  • 当然,我可以做到。但是,将名称、URL、描述放在哪里还不是很清楚……我认为它们应该放在电子邮件的正文中 - 我说的对吗?

标签: html html-email mailto subject


【解决方案1】:

首先,您的HTML 代码中有一些错误(input 标记未正确关闭),我设法纠正了它们。

您不需要form 标签来实现您的目标,您可以使用它,但a 标签就足够了。所以,我把&lt;input type="button" id="submit_link_button" value="Submit" onclick="emailLink()"&gt;改为&lt;a href="" id="contact-link"&gt;Send&lt;/a&gt;标签,它有href属性,接受'mailto'链接,该属性将由JavaScript填充所需的信息,我给了它一个@ 987654331@ 以通过JavaScript 引用它。

您曾经在input 中使用“内联事件处理程序”和type="button"(将被a 标记替换),这是一种不好的做法。相反,最好使用内置的addEventListener 方法将事件处理程序附加到您想要的任何元素。 Learn more 关于addEventListener 方法。

这是一个可运行的 sn-p 说明所有内容。

// select the 'a' tag that has the mailto link to reference later in the code.
var contactLink = document.getElementById('contact-link');
// add click event listener to that 'a' tag.
contactLink.addEventListener('click', function() {
  // get all the information required by getting the inputs and the select tags values.
  var newName = document.getElementById("newName").value,
      newURL = document.getElementById("newURL").value,
      newDescrip = document.getElementById("newDesrcip").value, 
      newCat = document.getElementById("newCat").value,
      /* the subject variable holds the subject of the email, change its value per your requirement. */
      subject = 'New Link Submitted',
      /* the queryString variable holds the email's subject and body to be used in the href attribute of the 'a' tag. the '\n' character is used to make a new line and it must be encoded, along with other special characters as the space, in a valid URL format, we'll be using the built-in 'encodeURI' method for that task. */
      queryString = '?subject=' + subject + '&body=Name: ' + newName + '\nURL: ' + newURL + '\nDescription: ' + newDescrip + '\nCategory: ' + newCat,
      /* the emailAddr variable holds the email which you want the email to be sent to. Change its value per your requirement. */
      emailAddr = 'test@exemple.com';
  /* assign the href attribute of the 'a' tag by the queryString variable's value prepended by the desired email adress, and encode all the resulted string in the URL format using the built-in 'encodeURI' method. */
  this.href = window.encodeURI('mailto:' + emailAddr + queryString);
  // just for testing to see the resulted href string. Delete this line in production phase.
  console.log(this.href);
});
<div id="myModal" class="modal" align="center">
<div class="modal-content">
<div class="modal-header">
  <span id="close" class="close">&times;</span>
  <h3>Submit New Link</h3>
</div>
<div class="modal-body">
  <p>Name: <input type="text" class="newName" id="newName" value="newName" / ></p>
  <p>URL: <input type="text" class="newURL" id="newURL" value="newURL" / ></p>
  <p>Description: <input type="text" class="newDesrcip" id="newDesrcip" value="" / ></p>
  <p>
    Category:
    <select id="newCat" required>
      <option value="A"> A </option>
      <option value="B"> B </option>
      <option value="C"> C </option>
      <option value="D"> D </option>
    </select>
  </p>
</div>
<br>
<!-- no Form tag is needed, and also the 'input' with 'type=button' is replaced by the next 'a' tag -->
<a href="" id="contact-link">Send</a>

Learn more 关于encodeURI 方法。

Ps:为了使所有这些工作,用户必须配置他的默认邮件应用程序,否则什么都不会发生。

希望我把你推得更远。

【讨论】:

  • 抱歉反馈延迟,我出乎意料地不在办公室。以上工作完美 - 一路上我学到了一些东西 - 非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 2011-06-24
  • 1970-01-01
相关资源
最近更新 更多