0

首先,我是一个 JS 新手。我有 form1 询问我正在序列化的用户电子邮件地址。我需要的是获取这个单一的数据字段并将其反序列化到一个更复杂的表单2中的字段中,当用户提交表单1时,该表单在灯箱中生成。

我已经完成了序列化,也完成了灯箱,但我不知道如何获取在 form1 中提交的电子邮件地址并使用该数据预先填充 form2 中的电子邮件字段。任何帮助,将不胜感激。

两种形式的代码如下。(省略灯箱,因为它不适用)。基本上,我需要将'form1'中的'email'放入'form2'中的'email'字段

<form action="#" id="form1">
   <label>email:</label> <input type="text" name="email" id="email"/> 
    <input type="submit" value="send" name="submit" />
</form>

<form action="#" id="form2">
   <label>Email:</label> <input type="text" name="email" id="email"/>
   <label>Zip Code:</label> <input type="text" name="zip" id="zip"/>
   <strong>Choose a Newsletter </strong>
   <ul><li><input type="checkbox" value="2" name="Weekly" id="Weekly"><label>Weekly</label></li>
<li><input type="checkbox" value="4" name="Daily" id="Daily"><label>Daily</label></li>
<li><input type="checkbox" value="8" name="Monthly" id="Monthly"><label>Monthly</label></li>
</ul>
<input type="submit" value="Subscribe" name="Subscribe" />
</form>
4

1 回答 1

0

如果您显示更多代码,我可以提供更多帮助,但这本质上是您需要做的。

您拥有email原始和序列化形式的价值。由于form2位于灯箱中,因此它确实在同一页面上。因此,您可以轻松更改form2.

您可以使用.val()jQuery 函数执行此操作,如下所示:$("#form2 #email").val(email);

现在显然您需要将 jQuery 选择器 ( #form2 #email) 替换为实际适合form2. 您还需要email.val()函数内更改为您分配为电子邮件地址的任何变量form1

编辑

通过添加您的代码,我可以提供更多 - 谢谢!

此代码将在您提交第一个表单时运行,并将 form1 的电子邮件值放入 form2。

$("#form1").submit(function(sent){

    // Sets the value of emailvalue to the current email address
    var emailValue = $("#form1 #email").val();
    // Places the value of emailValue into the email field in form2
    $("#form2 #email").val(emailValue);

});
于 2012-08-09T16:42:03.663 回答