如何将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