jquery .load 重命名表单


jquery .load renaming for form

这是一个页面A.html

<form name=form>
<input type=text id = txtA>
</form>

当我使用 jquery 加载到 B 时.html我加载多次。

<form name=form>
<input type=text id = txtA>
</form>
<form name=form>
<input type=text id = txtA>
</form>
<form name=form>
<input type=text id = txtA>
</form>
<form name=form>
<input type=text id = txtA>
</form>

如何将表单名称重命名为表单1,表单2,表单3...形式N所以我可以得到这个

<form name=form1>
<input type=text id = txtA>
</form>
<form name=form2>
<input type=text id = txtA>
</form>
<form name=form3>
<input type=text id = txtA>
</form>

当我提交时,我想使用 PHP 来检索数据。

for each form
    formi.txtA

这样,我可以唯一地获取每个表单数据。

这将重命名所有称为form的表单,并添加越来越多的后缀:

$('form[name="form"]').each(function(i) {
    this.name = 'form' + (i + 1);
});

逐个加载表单时,需要不同的方法:

var renamer = (function() {
    var i = 1; // private counter
    // return public interface
    return function(base) {
        $(base).find('form[name="form"]').each(function() {
            this.name = 'form' + i;
            ++i;
        });
    }
}());

每次添加新表单时,您都可以调用以下内容:

renamer('#divid');

其中'#divid'是表单所在的父节点的 jQuery 选择器。

更新

您似乎有多个动态加载的输入字段。无需加载全新的表单,只需加载<input />片段即可。

$(`#form`).load('http://www.example.com/inputfield?type=dob');

然后,服务器将返回一个片段:

<input name="dob" type="text" value="" />

然后将其插入到一个表单中,以便您可以将其提交给 PHP。

一旦所有加载完成,您就可以运行一个循环。

for(var i=0; i < $("form").size(); i++){
    $("form").eq(i).attr("name", "formName"+i);
}