gpt4 book ai didi

javascript - 如何向 HTML 表单添加一行?

转载 作者:行者123 更新时间:2023-12-02 19:56:12 25 4
gpt4 key购买 nike

我正在制作一个简单的应用程序,其中有一系列下拉菜单或文本框。一旦用户填写了这些内容,结果字符串就会连接起来并显示给用户。现在我需要一个简单的功能,用户可以按“+”按钮并添加一行。理想情况下,我需要复制上面的行(包括输入)。例如我有:

下拉菜单 文本框 文本框 下拉菜单

连接字符串

如何简单地复制上面的行(减去连接的字符串),包括用户输入的内容。如果更容易的话,现在只需添加上面的行就可以了。我正在使用 JavaScript...以下是表单的代码:

<form action="">
<input type="text" id="site" value="Site" title="Site" onfocus="clickclear(this, 'Site')" onblur="clickrecall(this,'Site')" />
<input type="text" id="placementdesc" value="Placement description" title="Placement description" onfocus="clickclear(this, 'Placement description')" onblur="clickrecall(this,'Placement description')" />
<input type="text" id="placementtype" value="Placement Type" title="Placement Type" onfocus="clickclear(this, 'Placement Type')" onblur="clickrecall(this,'Placement Type')" />
<input type="text" id="size" value="Size" title="Size" onfocus="clickclear(this, 'Size')" onblur="clickrecall(this,'Size')" />
<input type="text" id="pricing" value="Pricing" title="Pricing" onfocus="clickclear(this, 'Pricing')" onblur="clickrecall(this,'Pricing')" />
<select id="servetype" title="Serve Type">
<option>STD – Standard trafficking type</option>
<option>SSV – Site-served</option>
<option>PIX – Pixel</option>
<option>CCD – Click command</option>
<option>PCC – Pixel and Click command</option>
<option>RMV – Rich Media Video</option>
<option>RME – Rich Media Expand</option>
<option>RMO – Rich Media Other</option>
</select>
</form>

这很简单还是我需要使用 DOM 并使用 createElement 等?如果在表格中完成会更容易吗?

需要明确的是,我尝试不使用 JQuery...但如果这样做更容易,我愿意切换。

最佳答案

在每个输入旁边添加加号按钮,并为它们提供一个 addRow 类(例如)。

将函数映射到.addRow的点击事件:

$(".addRow").click(function(){
$(this)
.prev("input") // select the previous input element
.clone() // clone said element
.appendTo("#formID"); // append the clone to the form
});

请记住,这也会克隆输入的现有值,因此您可能需要在附加之前使用 val() 清除克隆的值。

关于javascript - 如何向 HTML 表单添加一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8603390/

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