Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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 映射不带索引的复选框的动态数组_Php_Jquery_Html_Dom - Fatal编程技术网

Php 映射不带索引的复选框的动态数组

Php 映射不带索引的复选框的动态数组,php,jquery,html,dom,Php,Jquery,Html,Dom,此问题进一步基于此处提出的问题: 我有一组动态行,每个行都有自己的输入字段。这些行可以动态添加到DOM中,因此我必须使用没有索引的输入数组(例如fieldname[]而不是fieldname[1]等等) 在这些行中使用复选框时会出现问题。由于未选中复选框时不会提交复选框,因此我无法知道哪个已提交复选框属于哪个行值 我的表格示例: <form> <div class="row"> <input type="text" name="product[]">

此问题进一步基于此处提出的问题:

我有一组动态行,每个行都有自己的输入字段。这些行可以动态添加到DOM中,因此我必须使用没有索引的输入数组(例如fieldname[]而不是fieldname[1]等等)

在这些行中使用复选框时会出现问题。由于未选中复选框时不会提交复选框,因此我无法知道哪个已提交复选框属于哪个行值

我的表格示例:

<form>
<div class="row">
     <input type="text" name="product[]">
     <input type="text" name="qty[]"> 
     <input type="checkbox" name="projectline[]"> 
</div>
<div class="row">
    <input type="text" name="product[]">
    <input type="text" name="qty[]">
    <input type="checkbox" name="projectline[]"> 
</div>
<div class="row">
     <input type="text" name="product[]">
     <input type="text" name="qty[]">
     <input type="checkbox" name="projectline[]"> 
</div>
</form>

我在这里找到了一个类似问题的答案:,但答案显然只适用于具有索引的数组

这里最好的方法是什么

编辑:


我还会在服务器端检查表单是否有错误,如果表单有错误,则将其重定向回服务器端,因此我需要能够根据提交的值“重建”表单。

我看到的一个技巧是在每个提交值为0的同一字段的复选框之前放置一个隐藏字段。这样,如果您选中复选框,它将用复选框值覆盖0值,但是如果您不选中,您将得到一个0表示未选中,而不是数据中的零


从保持索引总数的注释中得到的答案也可以起作用,但要根据修改DOM的方式和时间而复杂一些。

我最终为每一行分配了一个索引号,每次添加一行时都会生成一个新的随机id。我将jQuery用于克隆函数和事件绑定

下面是我的完整解决方案

这是我的原始表格:

<form>
<div class="row">
 <input type="text" name="product[0]">
 <input type="text" name="qty[0]"> 
 <input type="checkbox" name="projectline[0]"> 
</div>
</form>

有没有可能记录下你拥有的数量,并在放入索引时为它们分配索引?我同意QPaysTaxes。这似乎是最有意义的。我可能可以通过javascript实现这一点,但我希望有一个更优雅的解决方案。只要给他们一个值
我使用了这个解决方案,但在这种情况下它不起作用,因为我使用的是非索引数组。选中复选框时,这两个字段都将提交。
<div id="templaterow">
 <input type="text" name="product[%%index%%]">
 <input type="text" name="qty[%%index%%]">
 <input type="checkbox" name="projectline[%%index%%]"> 
</div>
<button id="addrow" value="add new row"/>
$('#addrow').on('click',function()
{
    //template row is cloned and given the right attributes:
    var clone = $('#templaterow').clone(true, true);
    $('.row').last().after(clone);
    clone.addClass('row').removeAttr('id');

    // the %%index%% placeholder is replaced by a random index number between 100 and 9999999
    clone.html(function (index, html) {
        var rndIndex = Math.floor((Math.random() * 9999999) + 100);
        return html.replace(new RegExp('%%index%%','g'),rndIndex);
    });
});