如何从单独的表单预填充数据电子邮件

时间:2018-04-09 04:03:53

标签: javascript html

我正在使用Formspree和Stripe Checkout。我希望将所有输​​入到Formspree表单中的内容<input type="email" name="Customer Email" placeholder="your email here">发送到data-email="example@mail.com"。这可能吗?我的代码如下所示:

<!-- Begin Stripe Form -->
<form method="POST">
  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="pk_test_6pRNASCoBOKtIshFeQd4XMUh" data-image="" data-amount="8500" data-name="some co." data-description="Custom Blanket" data-locale="auto"
    data-zip-code="true" data-billing-address="true" data-shipping-address="true" data-email="example@mail.com">
  </script>
  <script>
    document.getElementsByClassName("stripe-button-el")[0].style.display = 'none';
  </script>
  <button type="submit">Done</button>
</form>
<!-- End Stripe Form -->
<!-- Begin Formspree Form -->
<form action="https://formspree.io/xrqjnzam" method="POST">
  <input type="text" name="_gotcha" style="display: none">
  <input type="hidden" name="_next" value="https://example.com/blanket/thankyou.html" />
  <input type="hidden" name="_subject" value="New Order!" />
  <input type="hidden" name="_cc" value="x@somedomain.com" />
  <!-- <input type="hidden" name="_format" value="plain" /> -->
  <input type="email" name="Customer Email" placeholder="your email here">
  <button hidden type="submit">Send</button>
</form>
<!-- End Formspree Form -->

2 个答案:

答案 0 :(得分:1)

我就是这样做的。将任何ID分配给script代码,例如

<script id="script" .. ></script>

现在您需要从表单字段中获取email值并将脚本标记设置为自定义或任何属性。像

这样的东西
var email  = document.querySelector('input[type="email"]').value;
document.getElementById("script").setAttribute('data-email', email);

答案 1 :(得分:1)

要将输入的电子邮件发送到script元素属性,您必须添加一个事件监听器。

$(function(){
  var $email_field=$('[name="Customer Email"]');
  $email_field.on('change', function(){
    $('#script').attr('data-email', $email_field.val());
  });
});

如果不是因为脚本已经加载并使用默认电子邮件值mail@example.com进行初始化。在这种情况下,您需要在用户键入电子邮件地址并将其插入DOM后使用JavaScript创建脚本。

$(function() {
    var $stripe_form = $('#stripe_form');
    var $email_field = $('[name="Customer Email"]');
    $email_field.on('change', function() {
        $('<script>').attr({
            "src" : "https://checkout.stripe.com/checkout.js",
            "class" : "stripe-button",
            "data-key" : "pk_test_6pRNASCoBOKtIshFeQd4XMUh",
            "data-image" : "",
            "data-amount" : "8500",
            "data-name" : "Company",
            "data-description" : "Product",
            "data-locale" : "auto",
            "data-zip-code" : "true",
            "data-billing-address" : "true",
            "data-shipping-address" : "true",
            "data-email" : $email_field.val(),
        }).prependTo($stripe_form);
    });
});

但很明显,每次用户都会添加一个新的<script>来更改电子邮件。因此,如果您在用户输入电子邮件时隐藏表单的电子邮件部分并通过单击按钮进行确认,那将会更好。

&#13;
&#13;
$(function() {
  var $stripe_form = $('#stripe_form');
  var $email_section = $('#email_section');
  var $email_field = $('[name="Customer Email"]');
  var $email_confirm_button = $('#email_confirm_button');
  $email_confirm_button.on('click', function() {
    $email_section.slideUp();
    $stripe_form.show();
    $('<script>').attr({
      "src": "https://checkout.stripe.com/checkout.js",
      "class": "stripe-button",
      "data-key": "pk_test_6pRNASCoBOKtIshFeQd4XMUh",
      "data-image": "",
      "data-amount": "8500",
      "data-name": "Company",
      "data-description": "Product",
      "data-locale": "auto",
      "data-zip-code": "true",
      "data-billing-address": "true",
      "data-shipping-address": "true",
      "data-email": $email_field.val(),
    })
    .prependTo($stripe_form);
  });
});
&#13;
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

<form action="https://formspree.io/xrqjnzbm" method="POST">
  <input type="text" name="_gotcha" style="display: none">
  <input type="hidden" name="_next" value="https://example.com/thankyou.html" />
  <input type="hidden" name="_subject" value="New Order" />
  <input type="hidden" name="_cc" value="x@example.com" />
  <div id="email_section">
    <input type="email" name="Customer Email" placeholder="your email here">
    <button id="email_confirm_button" type="button">Continue to Payment</button>
  </div>
  <button type="submit" hidden>Send</button>
</form>

<form id="stripe_form" method="POST" style="display:none">
  <button type="submit">Continue to Next Step</button>
</form>
&#13;
&#13;
&#13;