简体   繁体   English

重用html表单值

[英]Reusing html form values

I have a two forms in the same page. 我在同一页面中有两种形式。 The first form is just a abbreviated version of the second form, ie, the first form has three fields and the second form has the same three fields plus an additional eight fields. 第一种形式只是第二种形式的缩写形式,即,第一种形式具有三个字段,第二种形式具有相同的三个字段以及另外的八个字段。

When the submit button is pressed on the first form it navigates to the second form location. 在第一个表单上按下提交按钮时,它将导航到第二个表单位置。 Is it possible to use the values from the first form to populate the three repeated values on the second form? 是否可以使用第一种形式的值来填充第二种形式的三个重复值?

You can utilize the "submit" event of the first form (or just the "click" event for a non-submitting button). 您可以利用第一种形式的“提交”事件(或仅对非提交按钮使用“单击”事件)。 Capture the value of the first three fields with Javascript, and set the value of the next form's fields. 使用Javascript捕获前三个字段的值,并设置下一个表单的字段的值。 Then you can transition to the next form. 然后,您可以转换到下一个表格。

$('#form-1').submit(function(e) {
    // Set the value of the 2nd form to the values of the 1st form's fields
    // Add as many as you like. You can also do validation here.
    $('#field-1-ext').val( $('#field-1').val() );
    $('#field-2-ext').val( $('#field-2').val() );

    // Show the second form, hide the first
    $('#form-1').fadeOut();
    $('#form-2').fadeIn();

    // Prevent normal form submission (navigating away from page)
    e.preventDefault();
    return false;
});

Here is a JSFiddle example: http://jsfiddle.net/muof06a0/1/ 这是一个JSFiddle示例: http : //jsfiddle.net/muof06a0/1/

I believe there are several ways for your problem.. The first ugly one would be to put your <form> tag above both form.. Like this: 我相信有几种方法可以解决您的问题。.第一个丑陋的方法是将您的<form>标记放在这两种形式之上。

<form action="#">
<input type="text" class="first" />
<input type="text" class="first" />
<input type="button" />

<!-- Put anything you need here -->

<input type="text" class="second" />
<input type="text" class="second" />
<input type="submit" />
</form>

The second one would be preparing some <input type="hidden" /> in the second form and upon clicking the first button, you can copy the value to the <input type="hidden" /> . 第二个将以第二种形式准备一些<input type="hidden" /> ,然后单击第一个按钮,您可以将值复制到<input type="hidden" />

<input type="text" class="first" />
<input type="text" class="first" />
<input type="button" />

<!-- Put anything you need here -->

<form action="#">
<input type="hidden" class="first" />
<input type="hidden" class="first" />
<input type="text" class="second" />
<input type="text" class="second" />
<input type="submit" />
</form>

OK, here's my simple JQuery version. 好的,这是我简单的JQuery版本。 Function below. 功能如下。 See http://jsfiddle.net/x7879Lt1/ for work test 参见http://jsfiddle.net/x7879Lt1/进行工作测试

// Parameters form1 and form2 are the ids of the form you're interested in.
function populate(form1, form2) {
    Loop through every element of form2
    $(':input', '#'+form2).each(function() {
//      alert(this.name + ': ' + this.value);

        // Look for form1 value
        var sub_value = ( $('#'+form1+' input[name='+this.name+']').val() );

        // If defined, set it, else move along, nothing to see
        if (sub_value) {
            this.value = sub_value;
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM