dongyi2006 2015-02-05 13:31
浏览 99
已采纳

映射没有索引的动态复选框数组

This question builds further on the question asked here: How to map dynamic array of input fields .

I have a dynamic set of rows with each it's own input fields. These rows can be dynamically added to the DOM, so I have to use input arrays without an index ( eg fieldname[] instead of fieldname[1] etc).

The problem occurs when I use checkboxes in these rows. Since checkboxes are not submitted when they are not checked, I see no way of knowing which submitted checkbox belongs to which row values.

Example of my form:

<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>

I found an answer to a similar problem here: php array of checkboxes , but the answer obviously only applies to arrays with an index.

What is the best approach here?

EDIT :

I also check the form for errors server-side and redirect it back if it is faulty, So I need to be able to 'reconstruct' the form based on the submitted values.

  • 写回答

2条回答 默认 最新

  • doudu22272099831 2015-02-05 17:58
    关注

    I ended up assigning an index number to each of the rows, generating a new random id each time a row is added. I used jQuery for the clone functions and event binding.

    Below is my complete solution.

    This is my original form:

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

    I have a template row that I use to make clones of:

    <div id="templaterow">
     <input type="text" name="product[%%index%%]">
     <input type="text" name="qty[%%index%%]">
     <input type="checkbox" name="projectline[%%index%%]"> 
    </div>
    

    A button to clone the row:

    <button id="addrow" value="add new row"/>
    

    And a function bound to the button:

    $('#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);
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)