Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当使用jquery动态添加行时,只有我的最后一个表单行在post中注册_Php_Jquery - Fatal编程技术网

Php 当使用jquery动态添加行时,只有我的最后一个表单行在post中注册

Php 当使用jquery动态添加行时,只有我的最后一个表单行在post中注册,php,jquery,Php,Jquery,我使用此代码向表单中添加行,问题是当表单发布时(用户通过单击加号图标使用JS添加行),它将只返回上一次发布的值: //Order Form $("#add").click(function() { counter++; var cln = $('#ordertable tbody>tr:last').clone(true); cln.find("[id^='prodcode'], [id^='meterage']").each(function(i, val

我使用此代码向表单中添加行,问题是当表单发布时(用户通过单击加号图标使用JS添加行),它将只返回上一次发布的值:

    //Order Form
$("#add").click(function() {
    counter++;
    var cln = $('#ordertable tbody>tr:last').clone(true);
    cln.find("[id^='prodcode'], [id^='meterage']").each(function(i, val) {
        val.id = val.id.match(/^([^0-9]+)[0-9]*$/)[1] + "" + counter;
    });
    cln.insertAfter('#ordertable tbody>tr:last');
    $('#ordertable tbody>tr:last input').val('');
    $('td.imgsample:last a').remove();
    return false;
});
形式呢

<form id="orderform" name"orderForm" action="tomypage.php" method="post">
<a  id="add">+</a>
 <table id="ordertable" width="533" border="0" cellspacing="0" cellpadding="2">
  <tbody>
    <tr>
      <td width="33%">Product Code (e.g 66203)</td>
      <td width="33%">mtrs sq Required (e.g 10)</td>
      <td width="33%">Preview Image</td>
    </tr>
    <tr class="item">
      <td class="prodcode"><input type="text" name="prodcode" id="prodcode" /></td>
      <td class="meterage"><input type="text" name="meterage" id="meterage" /></td>
      <td class="imgsample"></td>
    </tr>
    </tbody>
  </table>
<button>Submit</button>
</form>

+
产品代码(例如66203)
要求的mtrs平方米(例如10)
预览图像
提交

提交数据时,
元素按
名称
而不是
id
进行序列化。实际上,我会在这里删除
id
s,只处理
name
属性,如下所示:

cln.find("[name^='prodcode'], [name^='meterage']").each(function(i, val) {
    val.name = val.name.match(/^([^0-9]+)[0-9]*$/)[1] + "" + counter;
});
如果您出于其他原因需要
id
,只需在您当前拥有的内容中添加一个
.name
setter即可:

cln.find("[id^='prodcode'], [id^='meterage']").each(function(i, val) {
    val.name = val.id = val.id.match(/^([^0-9]+)[0-9]*$/)[1] + "" + counter;
});

谢谢你,尼克!事后看来,这是一个愚蠢的问题:)