【问题标题】:Is it possible to dynamically set the recipient of MAILTO: with only HTML and JavaScript?是否可以动态设置 MAILTO 的收件人:仅使用 HTML 和 JavaScript?
【发布时间】:2011-10-15 14:33:54
【问题描述】:

我正在创建一个仅使用 HTML 和 JavaScript 的表单,我希望能够通过更改 MAILTO: 地址或 mailto: 行的抄送地址来动态设置 POSTed 表单的收件人。如果可能,这将由文本框的值决定。此功能的目的是表单数据需要根据他们在下拉菜单中所做的选择发送到不同的管理器。 I have seen this done in one place (http://javascript.internet.com/forms/multiple-mailer.html) but my issue is that I am populating my dropdown from a JS array which when selected populates some text fields just like (http://www.webdeveloper.com/forum/showpost.php?p=984036&postcount=8)。所以我想做的是用我的下拉菜单填充一个隐藏的文本输入,然后从中提取来确定谁收到了电子邮件。

我应该注意,这是针对缺少桌面电子邮件客户端的企业 INTRAnet 站点,其中 IE 是唯一可用的浏览器。我无法使用服务器端技术,所以 mailto 是我唯一的选择,请不要浪费 cmets 告诉我这是多么糟糕的想法,哈哈。谢谢!

想法?

【问题讨论】:

    标签: javascript html dynamic mailto


    【解决方案1】:

    使用一些电子邮件地址打开邮件客户端:

    location = 'mailto:' + email_address;
    

    要设置a 元素的href,首先使用document.getElementById 之类的方法获取它,然后设置href 属性:

    document.getElementById('someLink').href = 'mailto:' + email_address;
    

    email_address 是一个包含适用电子邮件地址的字符串——您可以将其替换为获取下拉列表值的表达式:

    document.getElementById('someLink').href = 'mailto:' + document.getElementById('dropdown').value;
    

    【讨论】:

    • 我可能真的很笨,但我在实现这一点时遇到了一些困难。任何人都可以发布一个如何与基本表单标题匹配的示例:
    • document.getElementById('infoSubmit').action = 'mailto:' + email_address; 应该可以解决问题。
    【解决方案2】:

    您可以使用 Javascript 将 href 属性设置为包含电子邮件地址的字符串。

    【讨论】:

    • 你能提供一个例子或链接吗?
    【解决方案3】:

    是的,这是可能的。我的表单中有一个类似于此的选项框:

    <td><select name="Division" id="Division" onChange="dirChange()" tabindex="3">
    <option selected>Choose One</option>
    <option value="Communications">Communications</option>
    <option value="Legal Services">Legal Services</option>
    </select>        &nbsp;</td>
    

    它指的是一个javascript函数:

    function dirChange()
    {
    var i = document.all.Division.value;
    switch (i)
    {
    
        case "Communications":
            document.all.DeputyDirector.value = "Dave C.";
            document.all.form.action = "mailto:Dave.C@xxxxx.com?subject=Form";
            break;
    
        case "Legal Services":
            document.all.DeputyDirector.value = "Dixie P.";
            document.all.form.action = "mailto:Dixie.P@xxxxxx.com?subject=Form";
            break;
    
        default:
            document.all.DeputyDirector.value = "";
            break;
    
    }
    }
    

    然后,此 javascript 可帮助在文本框中填写所需信息,并将表单通过电子邮件发送给选定的人。

    <tr>
      <td class="announcementText"> <span class="style12"><span class="style16"><span class="style31">*</span></span></span>Division Deputy Dir.:  </td>
      <td><input name="DeputyDirector" type="text" id="DeputyDirector" style="background-color:#cccccc; color:black; font-weight:bold; border:0; overflow:visible" size="38"></td>
    
    </tr>
    
    <form onChange="dirChange()" method="post" enctype="text/plain" name="form" id="form"   onSubmit="return checkform(this);">
    

    客户选择通信部门后生成的 html 为: 对于文本框:

    <input name="DeputyDirector" type="text" id="DeputyDirector" style="background-color:#cccccc; color:black; font-weight:bold; border:0; overflow:visible" size="38" value = "Dave C.">
    

    对于形式:

    <form onChange="dirChange()" method="post" enctype="text/plain" name="form" id="form"   onSubmit="return checkform(this); action = "mailto:Dave.C@xxxxx.com?subject=Form">
    

    或者如果他们选择法律服务 对于文本框:

    <input name="DeputyDirector" type="text" id="DeputyDirector" style="background-color:#cccccc; color:black; font-weight:bold; border:0; overflow:visible" size="38" value = "Dixie P.">
    

    对于形式:

    <form onChange="dirChange()" method="post" enctype="text/plain" name="form" id="form"   onSubmit="return checkform(this); action = "mailto:Dixie.P@xxxxxx.com?subject=Form">
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 2020-07-28
      • 2011-12-30
      • 1970-01-01
      • 2013-07-05
      • 2012-10-18
      • 1970-01-01
      相关资源
      最近更新 更多