【问题标题】:Using jQuery to read from .serialize to populate a form使用 jQuery 从 .serialize 读取以填充表单
【发布时间】:2012-08-09 16:12:50
【问题描述】:

首先,我是一个 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>

【问题讨论】:

    标签: jquery forms serialization


    【解决方案1】:

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

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

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

    现在显然您需要将 jQuery 选择器 (#form2 #email) 替换为实际适合 form2 中的新电子邮件字段的内容。您还需要将 .val() 函数中的 email 更改为您从 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);
    
    });
    

    【讨论】:

    • 感谢您的反馈和建议的解决方案。我添加了上述两种形式的代码,希望得到更适合我具体情况的答案。谢谢
    • @NateLandau 我添加了一些代码,用于复制电子邮件的值并放入第二个电子邮件字段。
    • @NateLandau 如果这对您有所帮助,我将非常感谢您将其标记为答案。
    猜你喜欢
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多