简体   繁体   English

同时打开mailto并提交表格

[英]Simultaneously open mailto and submit form

At management's request, I need to open the user's email client and submit a form on a button click. 应管理层的要求,我需要打开用户的电子邮件客户端,然后单击按钮提交表单。 I initially had 我最初有

window.location = "mailto:email@example.com";

as the callback to the click event for the submit input, but this doesn't work. 作为提交输入的click事件的回调,但这不起作用。 It seems like the form submits to quickly. 表单似乎很快就提交了。

Using window.open does work, but it creates a blank window that is undesirable. 使用window.open确实可以,但是会创建一个空白窗口,这是不希望的。

I also had the idea to prevent and delay the form submission as in 我也有防止和延迟提交表单的想法,就像

window.location = "mailto:personalcounselor@gleim.com";
setTimeout(function () { 
    $(this).closest('form').trigger('submit');
}.bind(this), 1000); 
e.preventDefault();

This also works, but it seems sketchy to me. 这也可以,但是对我来说似乎很粗略。

Is there any way to submit the form and open a mailto link at the same time? 有什么办法可以同时提交表格并打开mailto链接?

I don't know if this will work, but I am sure with a bit of tweaking it should work and have the desired result which you are after (it was too long to fit in a comment, if it will not work I will gladly delete it!); 我不知道这是否行得通,但是我敢作一点调整,以确保它能达到预期的效果(太长了,无法发表评论,如果行不通,我很乐意删除它!);

$("form").on("submit", function() {
     window.onbeforeunload = function() {
         window.location = "mailto:email@example.com";
     };
});

Simply put when the form is submitted, set the onbeforeunload event to change the location to the mailto. 简单地说,提交表单时,设置onbeforeunload事件以将位置更改为mailto。 I think doing it this way will only make the mailto open if the form is submitted rather than when a user just navigates away. 我认为以这种方式进行操作只会在提交表单时才打开邮件,而不是在用户离开时才打开。

I don't know if this will work, or how hacky it is, but thought I would throw in my two cents! 我不知道这是否行得通,或者它有多怪异,但我想我会花两美分!

UPDATE UPDATE

On form submit, mailto from javascript with form values 在表单提交时,使用表单值从JavaScript发送mailto

This does seem to work and verified by others. 这似乎确实可行,并已被其他人证实。

$("input[type=submit]").click(function(){
     window.location.href = "mailto:email@example.com";
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM