简体   繁体   English

如何在HTML表单中添加行?

[英]How can I add a row to an HTML form?

I'm making a simple app where there are a series of either drop downs or text boxes. 我正在创建一个简单的应用程序,其中有一系列的下拉或文本框。 Once the user has filled these in the resulting strings are then concatenated and displayed to the user. 一旦用户填写了这些字符串,然后连接并显示给用户。 Now I need a simple function where the user can press a '+' button and adds a row. 现在我需要一个简单的功能,用户可以按下“+”按钮并添加一行。 Ideally I need the row above to be copied (including inputs). 理想情况下,我需要复制上面的行(包括输入)。 So for example I have: 所以我举例说:

dropdown textbox textbox dropdown dropdown textbox textbox textbox textbox dropdown

concatenated string

How can I simply copy the row above (minus the concatenated string), including what the user has inputted. 我怎样才能简单地复制上面的行(减去连接的字符串),包括用户输入的内容。 If it's easier simply adding the row above for now would be good. 如果现在简单地添加上面的行就更容易了。 I'm using JavaScript...here's the code for a form for example: 我正在使用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>

Is this simple or would I need to use the DOM and use createElement etc? 这很简单,还是需要使用DOM并使用createElement等? Would it be easier if this was done in a table? 如果这是在表格中完成的话会更容易吗?

Just to be clear, I'm trying NOT to use JQuery...I'm willing to switch though if it's easier to do it this way. 为了清楚起见,我正在尝试不使用JQuery ......我愿意切换,如果这样做更容易。

Add the plus button next to each input and give them a class of addRow (for example). 在每个输入旁边添加加号按钮,并为它们提供一个addRow类(例如)。

Map a function to .addRow 's click event: 将函数映射到.addRow的click事件:

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

Keep in mind this will clone the existing values of the inputs too, so you may want to clear the clone's value using val() before appending it. 请记住,这也将克隆输入的现有值,因此您可能希望在附加之前使用val()清除克隆的值。

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

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