On form submit, mailto from javascript with form v

2019-07-20 13:39发布

问题:

I have a form, and when the form is submitted (input type="submit"), i would like to open the clients default mail-browser with a pre-populated email-message.

So when the user clicks submit two things need to happen. Open email and submit form.

Also, how can i use the values entered in the form to prepopulate the email?

I'm new to javascript-jquery so please, any code example would be of great help!

Thanks for your help!

回答1:

Before submitting the form you could do:

 window.location.href = 'mailto:nicola@mio.it';

this will open the predefined mail client and you can also prefill some field. look at the mailto sintax here or post some more info so that we can help you;

This could be done like this :

$('input[type=submit]').click(function(){
     window.location.href = "mailto:" + $('#email').val();
});


回答2:

you can prepopulate form with

$("textarea").val('Your message here! You\' have to strip \'');

if it's default email clien't, I'm afraid it's not possible



回答3:

I have used a code like this when I needed to send via mailto using my local email client, it may help:

<html>
<head>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<form id="myform" enctype="text/plain" action="test.php" method="post" >
	<input type="text" value="value1" id ="field1" name="field1">
	<input type="checkbox" value="valuel2" id ="field2" name="field2" checked>
	<input type="checkbox" value="value3" id ="field3" name="field3" >
	<textarea id="myText" name ="texty">
	    Lorem ipsum...
	</textarea>
	<button onclick="sendMail(); return false">Send</button>
</form>
<script>
function sendMail() {
	$myform = $('#myform');
	$myform.prop ('action','mailto:mymail@mydomain.es');
	$myform.submit();
}
</script>

</body>
</html>