gpt4 book ai didi

php - jquery .load 重命名表单

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:30 25 4
gpt4 key购买 nike

这是一个页面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>

如何将表单名称重命名为 form1,form2,form3...formN这样我就可以得到这个

<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。

关于php - jquery .load 重命名表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10732326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com