首先,你的工作中有一些错误
HTML
input
标签没有正确关闭),我设法纠正他们。
你不需要一个
form
为了实现你的目标,你可以使用它,但是
a
标签就足够了。所以
<input type="button" id="submit_link_button" value="Submit" onclick="emailLink()">
<a href="" id="contact-link">Send</a>
href
接受“mailto”链接的属性,该属性将由
JavaScript
我给了它一个
id="contact-link"
JavaScript语言
.
输入
具有
type="button"
一
这是一个坏习惯。相反,最好使用内置的
addEventListener
Learn more
关于
附加侦听器
方法。
下面是一个可运行的片段,它演示了所说的全部内容。
// 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">×</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
附言: