Is it possible to dynamically set the recipient of MAILTO: with only HTML and JavaScript?

大兔子大兔子 提交于 2019-12-01 00:19:55

To open the mail client with some email address:

location = 'mailto:' + email_address;

To set the href of an a element, first get it using something like document.getElementById, then set the href property:

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

email_address is a string containing the applicable email address -- you can replace this with an expression that gets the value of the dropdown:

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

You can just set the href attribute to a string containing an email address using Javascript.

Yes, it is possible. I have an option box in my form similar to this:

<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>

It refers to a javascript function:

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;

}
}

This javascript then helps fill in the needed information on a text box and for the form to email to the selected person.

<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);">

The resulting html for the client, once they select a communications division is: for the text box:

<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.">

and for the form:

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

or if they select legal services for the text box:

<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.">

and for the form:

<form onChange="dirChange()" method="post" enctype="text/plain" name="form" id="form"   onSubmit="return checkform(this); action = "mailto:Dixie.P@xxxxxx.com?subject=Form">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!