jquery - 动态地向表单添加字段 - 当您提交表单时,post缺少数据

时间:2016-11-08 15:26:25

标签: javascript php jquery forms

背景资料

我有一个表单,除其他外,允许用户使用jquery动态添加名字/姓氏文本字段。

问题

当用户提交数据时,附加字段& POST中缺少通过jquery创建的数据。

测试详情

使用“addname”按钮,除了表单上的默认fname / lname字段外,我还添加了两个或三个附加名称。然后我提交数据。数据缺少动态添加的字段。

我尝试/检查过的内容

  1. 确保当我添加新行的“名字”/“姓氏”文本框时,每个控件都有一个唯一的“名称”。正如您所看到的,我将自定义类名称分配给第一个名称字段,然后在jquery中我计算我已经拥有多少个名字字段。我将该值递增1,然后将新数字烘焙到我创建的新文本字段的名称中。使用F12调试窗口,我可以看到HTML代码将这些计数器添加到名称中。

  2. 确保动态添加的数据行在@Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case PLAY_SONG: { songClick(MyView); } break; case ADD_TO_PLAY_LIST: { contextMenuFlag = false; songView.post(new Runnable() { @Override public void run() { songView.showContextMenu(); } }); } break; case CREATE_PLAYLIST:{ addToPlayList(); } break; default: // so user tapped on a specific playlist // get the playlist database id by reverting the // step for building the item id int playlist_dbid = item.getItemId() - 100; doSomethingWithPlaylist(playlist_dbid); } return super.onContextItemSelected(item); }

  3. 确保我在FORM中有一个提交按钮。

  4. 我不确定我还应该检查什么。

    如果您有任何建议,我会很感激。

    <FORM>
    //Here's of the two jquery methods I have that allows users to dynamically add new fields / data to the form: 
    
    $("#addname").click(function() {
      var numItems = $('.widgetname').length; //count all items with widgetname class
      numItems += 1;
      htmlstring = "<tr>";
      htmlstring += "<td><input class='form-control widgetname' type='input' placeholder='First Name' name='fname" + numItems + "'/></td>";
      htmlstring += "<td colspan='2'><input class='form-control' type='input' placeholder='Last Name' name='lname" + numItems + "' /></td>";
      htmlstring += "</tr>";
      $("#tcsection").before(htmlstring);
    });

1 个答案:

答案 0 :(得分:0)

尝试将表格移到桌子外面。

我现在看到奇怪的HTML:

<tr> <?php echo form_open( 'widget/assign'); ?> <tr> 

我预期

<?php echo form_open( 'widget/assign'); ?>
<table class="table table-bordered table-hover">
  <tr>
    <td colspan="3" class="btn-warning">
      <?php echo validation_errors(); ?>
    </td>

PS:你永远不应该使用name =&#34;提交&#34;对于任何表单元素,如果您打算使用脚本提交表单。它将覆盖任何提交事件处理程序

相关问题