我已经使用 javascript 创建了一个 html 表单,该表单必须在 IE 上运行才能通过使用 mailto 操作触发 Outlook,但对于我传递给 Outlook 的字符串,所有空格都会自动替换为 (+) 符号。下面是我的代码:
<script>
var i= 'Product name:';
var idproduct=i.split('+').join(' ')
function beforeSubmit() {
var Product = document.getElementById("Product_Name");
var Email = document.getElementById("Email_Address");
var body = document.getElementById("body");
body.value = idproduct+ Product.value +"\n";
}
</script>
<input name="Subject" size="78" id="Subject" type="hidden" value=" Car Notification" /><br/>
但是我得到的输出是:
电子邮件主题:
Car+Notification
电子邮件正文:
Product+name:Honda
我的预期输出没有空格的所有加号。有人对这个问题有想法吗?
最佳答案
我们可以像这样编写 mailto 链接以避免“+”符号:mailto:somebody?subject=etc&body=etc
。我们将主题和正文作为查询参数发送。您可以检查下面的示例,它在 IE 中有效:
function beforeSubmit() {
var Product = document.getElementById("Product_Name").value;
var Email = document.getElementById("Email_Address").value;
var eTo = encodeURI(Email);
var eSubj = encodeURI("Car Notification");
var eBody = encodeURI(Product + "\n" + "anotherline");
var email = "mailto:" + eTo + "?subject=" + eSubj + "&body=" + eBody;
document.getElementById("myform").href = email;
}
<form>
<input id="Email_Address" type="text" value="somebody@example.com" />
<input id="Product_Name" type="text" value="enter your message here" />
<a href="" id="myform" onclick="beforeSubmit()"><input type="button" value="submit" /></a>
</form>
关于javascript - 如何避免 HTML javascript 字符串空格带有 (+) 符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57593104/