doupai1876 2018-12-11 18:47
浏览 244
已采纳

如何在数据表列中获取填充文本输入字段的值

I have a data table that I populate text fields in a column for every row. I tried to get the value from these text fields but it always take the first column's text field value since as I assume id is always the same for every text field populated. How can I solve this issue ? Do I have to also populate dynamic id names for every text field ? I am getting the value with jQuery by the way.

With this piece of code:

var qty = $("#input").val();

And I am going to send this "qty" variable to a php code via an Ajax call. Ajax call is working fine, sending the variable, I have checked it so it is not the problem.

Table declaration code:

var table3 =  $("#last3").DataTable({

                                    "columnDefs": [
                                {
                                "targets": 4,
                                "data": null,
                                "defaultContent": '<input id="input" type="number"  value="" min="1" max="9"></input>'
                                },

                                {
                                "targets": 3,
                                "data": null,
                                "defaultContent": '<input type="button" value="Add" class="btn-add" style="width:100%; height:100%;"></input>'
                                },

                                { "searchable": false, "targets": 0 } ],

                                select:false,

                                "scrollY": "32vh",
                                "scrollCollapse": true,
                                "paging": false,
                            });

EDIT 1: I have this piece of code, this dynamically create a cell value and increment it by one, so basically creates an index column. I just wonder is there a way to modify this so that instead of populating the cell.innerhtml, populates the "id" attribute somehow. so this way I would have unique id attr for each text field. (does not have to be id though, I can also get the value by "name" attr.)

table3.column(4, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {cell.innerHTML = i+1;}
  • 写回答

1条回答 默认 最新

  • dsjq6977 2018-12-12 17:03
    关注

    You need to bind the button add after table draw, because of paging, searching etc. And as i sayied in comments, inside your iterator you have the cell param, you can search inside that to find your input, and then deal with it.

    Maybe this help you:

    $(document).ready(function() {
    
    
      var table3 = $("#last3").DataTable({
    
        "columnDefs": [{
            "targets": 4,
            "data": null,
            "defaultContent": '<input id="input" type="number" class="table-input"  value="" min="1" max="9"></input>'
          },
    
          {
            "targets": 3,
            "data": null,
            "defaultContent": '<input type="button" value="Add" class="btn-add" style="width:100%; height:100%;"></input>'
          },
    
          {
            "searchable": false,
            "targets": 0
          }
        ],
    
        select: false,
    
        "scrollY": "32vh",
        "scrollCollapse": true,
        "paging": false,
      }).on('draw.dt', function() {
        $("#last3 tr").on("click", ".btn-add", function() {
          var data = table3.DataTable().row($(this).closest('tr')).data();
          console.log(data);
        })
      });
    
    
      table3.column(4, {
        search: 'applied',
        order: 'applied'
      }).nodes().each(function(cell, i) {
        var $input = $(cell).find('input');
        $input.attr({
          'id': ($input.attr('id') + i),
          'value': i
        });
      }).draw();;
    
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.18/css/jquery.dataTables.min.css" rel="stylesheet" />
    <script src="https://cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js"></script>
    
    
    
    <table id="last3" class="display">
      <thead>
        <tr>
          <th>Column 1</th>
          <th>Column 2</th>
          <th>Column 3</th>
          <th>Column 4</th>
          <th>Column 5</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Row 1 Data 1</td>
          <td>Row 1 Data 2</td>
          <td>Row 1 Data 3</td>
          <td>Row 1 Data 4</td>
          <td>Row 1 Data 5</td>
        </tr>
        <tr>
          <td>Row 2 Data 1</td>
          <td>Row 2 Data 2</td>
          <td>Row 2 Data 3</td>
          <td>Row 2 Data 4</td>
          <td>Row 2 Data 5</td>
        </tr>
        <tr>
          <td>Row 3 Data 1</td>
          <td>Row 3 Data 2</td>
          <td>Row 3 Data 3</td>
          <td>Row 3 Data 4</td>
          <td>Row 3 Data 5</td>
        </tr>
        <tr>
          <td>Row 4 Data 1</td>
          <td>Row 4 Data 2</td>
          <td>Row 4 Data 3</td>
          <td>Row 4 Data 4</td>
          <td>Row 4 Data 5</td>
        </tr>
      </tbody>
    </table>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置