通过get使用javascript或jquery将数据从一个表单推送到另一个表单


Pushing data from one form to another using javascript or jquery through the get

我在我的网站上有一个电子邮件的初始注册框,用 php 构建。我正在使用 magento,所以我不能简单地从下一页的电子邮件字段中回显变量。这是我目前习惯这样做的唯一方法。

<form action="<?php echo $this->getUrl('email_preferences') ?>" method="get" id="newsletter-validate-detail" class="footerNewsletter">
    <label for="newsletter" class="footerHeads"><?php echo $this->__('Connect With US') ?></label>
    <div class="inputTextContainer">
        <input name="email" type="text" id="newsletter" placeholder="<?php echo $this->__('enter email for specials and updates!') ?>"
               title="<?php echo $this->__('Enter email for specials and updates!') ?>" class="required-entry validate-email">
        <i class="icon-mail"></i>
    </div>
    <input type="hidden" name="source" value="nt" />
    <button type="submit" class="button" style="display:none;" title="<?php echo $this->__('Subscribe') ?>"><span><span><?php echo $this->__('Subscribe') ?></span></span></button>
</form>
<script type="text/javascript">
//<![CDATA[
    var newsletterSubscriberFormDetail = new VarienForm('newsletter-validate-detail');
//]]>
</script>

第二页将有一个标准输入框,如下所示:

<input type="text" name="email" size="40" maxlength="100" value=""/>

我知道在 php 中我可以使用值和回显$email。

我在想在javascript中我可以使用这样的东西:

<script type="text/javascript">
document.getElementById("email").innerhtml = window.location.search;
</script>

不过,我不确定如何使用document.write来填写电子邮件输入。

尝试使用jQuery,它会让生活变得更加轻松。假设您的页面网址如下所示:www.example.com/page.php?email=me@email.com

<script type="text/javascript">
$(function() {
    $('input[name="email"]').val(window.location.search.replace('?email=', ''));
});
</script>

如果您正在访问www.example.com/page.php?email=you@domain.com则"名称"输入字段将获得值"you@domain.com"。

示例 fidde:https://jsfiddle.net/f8817t4q/(不过,出于演示目的,必须用静态字符串替换window.location.search)。