如何使用javascript将数据数组编辑为复选框

问题描述 投票:-4回答:1

表格HTML

 <table id="example3" class="display" cellspacing="0" width="100%">
    <thead>
      <tr>
         <th></th>
         <th>Nama</th>
         <th>Unit</th>
      </tr>
    </thead>
 </table>

Javascript以选择行

$('#frm-example').on('submit', function(e){ //on submit
    var form = this;
    var rows_selected = table3.column(0).checkboxes.selected();

        // Iterate over all selected checkboxes
        $.each(rows_selected, function(index, rowId){
           // Create a hidden element 
           $(form).append(
              $('<input>')
              .attr('type', 'hidden')
              .attr('name', 'id[]')
              .val(rowId)
           );
       });

我的数据库

+-----------+--------------+---------------+-----------+-------------+-----------+------------------------------+---------------------+
| id_survey | judul_survey | status_survey | id_target | id_kategori | responden | detail_target                | tgl_survey          |
+-----------+--------------+---------------+-----------+-------------+-----------+------------------------------+---------------------+
|       130 | tes          | terbit        |         3 |           2 |         2 | 198411162009101002,H76215021 | 2017-12-19 15:08:35 |
+-----------+--------------+---------------+-----------+-------------+-----------+------------------------------+---------------------+
1 row in set (0.07 sec)

字段detail_kategori中的数据复选框,我如何将其放入表单复选框进行更新?

好的,我从这里获取代码复选框https://jsfiddle.net/gyrocode/07Lrpqm7/我想修改代码进行编辑

javascript php jquery html checkbox
1个回答
0
投票

你可以用jQuery这样做:

$("#frm-example").submit(function() {
    var form = $(this);
    $("#table3 :checkbox:checked").each(function() {
        form.append($("<input>", {
            type: "hidden",
            name: "id[]",
            value: this.id
        }));
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.