如何在HTML表单中添加行?

时间:2011-12-22 11:42:08

标签: javascript html forms

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

dropdown textbox textbox dropdown

concatenated string

如何简单地复制上面的行(减去连接的字符串),包括用户输入的内容。如果现在简单地添加上面的行就更容易了。我正在使用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 ......如果这样做更容易,我愿意转换。

1 个答案:

答案 0 :(得分:2)

在每个输入旁边添加加号按钮,并为它们提供一个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()清除克隆的值。