如何将HTML表单数据添加到mailto链接 [英] How do I add HTML form data to a mailto link

查看:69
本文介绍了如何将HTML表单数据添加到mailto链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表单,该表单允许用户键入其姓氏和名字,然后提交.我想输入他们输入的名字和姓氏,然后将这些数据放在mailto链接中.下面的代码是我所拥有的.我在做什么错了?

I am trying to create a form that allows the user to type their first and last name, then submit it. I want to take the first and last name they entered and put that data in a mailto link. The code below is what I have. What am I doing wrong?

<div class="form">
    <form>
        <input type="text" name="firstname" placeholder="First Name">
        <input type="text" name="lastname" placeholder="Last Name">
        <button type="submit" class="submit" onclick="submitForm()"><i class="material-icons md-24">play_circle_filled</i></button>
    </form>
    <script>
    function submitForm{
        var fname = get.getElementsByName("firstname").value;
        var lname = get.getElementsByName("lastname").value;
        window.open("mailto:someone@example.com?subject=Test%20Email&body=First%20Name:%20"+fname+"%20Last%20Name:"+lname"");
    }
    </script>
</div>

推荐答案

尝试一下

function submitForm(form) {
  window.open("mailto:someone@example.com?subject=Test%20Email&body=First%20Name:%20" + form.firstname.value + "%20Last%20Name:" + form.lastname.value);
  return false; /* cancel submit or else page reloads */
}

<form onsubmit="return submitForm(this);">
  <input type="text" name="firstname" placeholder="First Name" />
  <input type="text" name="lastname" placeholder="Last Name" />
  <button type="submit" class="submit"><i class="material-icons md-24">play_circle_filled</i></button>
</form>

顺便说一句,以下是您的代码中的错误.

BTW, below are mistakes in your code.

:"+lname""); /*unwanted "" at the end*/

在这里,您有多项更正:

Here, you have multiple corrections:

get.getElementsByName("firstname").value

不是get.而是document.getElementsByName()也会返回NodeList而不是一个HTML元素,因此同样地,您将需要使用索引,所以最终得到document.getElementsByName("firstname")[0].value

It is not get. instead it is document., also getElementsByName() will return you NodeList instead of just one HTML Element, so likewise you will need to use index, so you end up with document.getElementsByName("firstname")[0].value

这篇关于如何将HTML表单数据添加到mailto链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆