简体   繁体   English

使用jQuery在表格的HTML表单中添加字段

[英]Add a field to an HTML form in a table using jQuery

I have a table, which can contain arbitrarily many rows. 我有一个表,可以包含任意多行。 Each of these rows holds an element of a form field. 这些行中的每一行都包含一个表单字段的元素。 Initially, it looks like this: 最初,它看起来像这样:

<table border="2" id="familyHeader">
  <tr>
    <th>Description</th>
    <th>Still associated?</th>
  </tr>
  <tr id="addNewChildRow"> 
    <td> 
      <input type="text" ....> 
      <input type="text" ....>
    </td> 
    <td> 
      <input type="submit" ....>  <!-- this is intentionally not part of a form element -->
    </td> 
  </tr>

<form id="family" method="post" action="php/update_children.php">
  <tr id="updateChildrenRow">
    <td> > > > > > > </td>
    <td> 
      <input type="submit" id="updateChildren" name="updateChildren" class="updateChildren" value="Update Children"/> 
    </td>
  </tr>
</form>
</table>

So basically the top part allows you to enter details - there is a capture on the first "submit" button which adds rows to the table. 因此,基本上,顶部允许您输入详细信息-第一个“提交”按钮上有一个捕获,该按钮向表中添加了行。 It adds them before the final row (so hopefully within the form element) using this JavaScript/jQuery: 它将使用此JavaScript / jQuery将它们添加到最后一行之前(因此希望在form元素内):

function insert_child(id, type, snumber, serialnumber)
{
    $('#updateChildrenRow').before("<tr> <td>"+ type+" | "+snumber+" | "+serialnumber +"</td>"
    //$('#family').append("<tr> <td>"+ type+" | "+snumber+" | "+serialnumber +"</td>"
       + "<td> <input type=\"hidden\" name=\""+id+"\" value=\"false\" />" 
       + "<input type=\"checkbox\" checked=\"true\" name=\""+id +"\" value=\"true\"/> </td>"
       + "</tr>");
}

This works, in that it inserts the element in the right place, but when we submit the form, the data is not POSTed. 这行得通,因为它在正确的位置插入了元素,但是当我们提交表单时,数据没有被发布。 However, if we move the form element opening to the second line and submit, the "addNewChildRow" buttons are POSTed (which I presume shows my posting logic works, but it's just not detecting the dynamically added fields). 但是,如果我们将表单元素的开头移至第二行并提交,则“ addNewChildRow”按钮将被过帐(我以为这显示了我的过帐逻辑,但它只是未检测到动态添加的字段)。

As you can see, I have tried appending to the form itself rather than the table, and this hasn't worked (they aren't displayed) - does anyone know what the trick is to dynamically add form elements to a table where they will be "detected" by the form which surrounds it? 如您所见,我尝试附加到表单本身而不是表格,但这没有用(它们没有显示)-有谁知道将表格元素动态添加到表格中的窍门吗?被其周围的形式“发现”?

Thanks :) 谢谢 :)

The problem here is not about 'detecting' added fields - this is greatly work always. 这里的问题不是关于“检测”添加的字段-总是很有效。 Your real problem is what you put the 'form' tag as a child of 'table', but this is not allowed and treats as an error by browser. 真正的问题是将“ form”标记作为“ table”的子项放置的,但这是不允许的,并且被浏览器视为错误。 Browser tries to fix it - and cant do anything more then immediatelly close 'form' tag and trying to move all it content outside. 浏览器尝试对其进行修复-无法执行任何操作,然后立即关闭“ form”标签并将其所有内容移至外部。 (To give you as much as he can). (尽可能多地给你)。 After that it found a lost '/form' and just wipe it out. 之后,它发现丢失的“ / form”,然后将其清除。

Your markup is converted to this: 您的标记将转换为:


<table border="2" id="familyHeader">
 .......
<form id="family" method="post" action="php/update_children.php"></form>

  <tr id="updateChildrenRow">
    <td> > > > > > > </td>
    <td> 
      <input type="submit" id="updateChildren" name="updateChildren" class="updateChildren" value="Update Children"/> 
    </td>
  </tr>
</table>

Now, your new inputs just created outside a form. 现在,您刚刚在表单外部创建的新输入。

To fix it, you can do one of the following: 要解决此问题,您可以执行以下任一操作:

  • Put the entire table into the 'form' tag. 将整个表格放入“ form”标签。 The easiest way, but in this case you will have two additional inputs (not a problem - just dont add the name attribute - and they will not be POSTed), and you must change type of the first button from 'submit' to 'button' so it will not trigger form submitting. 最简单的方法,但是在这种情况下,您将有两个附加输入(这不是问题-只需不添加name属性-并且它们将不会发布),并且必须将第一个按钮的类型从“提交”更改为“按钮” ',因此它不会触发表单提交。
  • Put the 'form' tag inside one of the 'td' or entire form outside of table. 将“表单”标签放在表外的“ td”之一或整个表单中。 In this case you need change the way how you create attitional fields to make it look exactly as you want. 在这种情况下,您需要更改创建修饰字段的方式,以使其完全符合您的期望。 (remember what you cannot add new 'td' directly to form - you must create a new table inside a form and add cells and rows to it. You can style everything to get what you want. (请记住您不能直接在表单中添加新的'td'的内容-您必须在表单内创建一个新表并向其中添加单元格和行。您可以设置所有样式,以获得所需的内容。

PS. PS。 Also, you have the same name for newly created hidden element and checkbox. 此外,新创建的隐藏元素和复选框的名称相同。 Be sure what this is exactly what you want - behavior can be unexpected. 确保这正是您想要的-行为可能是意外的。

尝试执行$ .ajax(type post)或$ .post并将$('#family')。serialize()作为数据传递。

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

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