获取表Javascript的隐藏输入值(Get the values of a hidden input a table Javascript)

编程入门 行业动态 更新时间:2024-10-21 11:47:23
获取表Javascript的隐藏输入值(Get the values of a hidden input a table Javascript)

假设我有一个这样的表:

<table id="parameters" width="100%" border="1">
  <thead>
    <tr>
      <th>Date</th>
      <th>Identity</th>
      <th>Names</th>
      <th>Quantity</th>
      <th>Concept</th>
      <th>Select</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2016-08-10<input type="hidden" name="date[]" value="2016-08-10"></td>
      <td>12345678<input type="hidden" name="identity[]" value="12345678"></td>
      <td>Pepito Perez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>daytime<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check[]" value="12345678"></td>
    </tr>
    <tr>
      <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
      <td>24681012<input type="hidden" name="identity[]" value="24681012"></td>
      <td>Camilo Sanchez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>daytime<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check[]" value="24681012"></td>
    </tr>
    <tr>
      <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
      <td>369121518<input type="hidden" name="identity[]" value="369121518"></td>
      <td>Pepito Perez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>daytime<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check" value="369121518"></td>
    </tr>
  </tbody>
  </table> 
  
 

此表是根据查询生成的,复选框的id是每个员工的标识值。

我想只从数组中选中的复选框中获取隐藏输入的值,然后通过ajax发送该数据,然后将它们保存在数据库中:

//Here, when clicking the button confirm searches the table that are marked confirm.on('click', function(){ //Save in result inputs with marked checkbox $("#parameters tbody tr td input[name=check]:checked").each(function(){ var result = []; $(this).closest('tr').find('input[type="hidden"]').each(function(){ result.push($(this).serializeArray()); }); //sending the object by ajax $.ajax({ type: "POST", url: getBaseUri()+'/payroll/createAll/', data: {datas: result}, cache: false, success: function (response) { var table = tableConfirm; var url = table.data('source'); clearFom(); table.dataTable().fnReloadAjax(url); }, error: function (response) { console.log(response); } }); });

我想知道我做错了什么,如果有更好的方法通过ajax发送数据将它们存储在数据库中,因为我收到了这样的对象:

$datas = Array ( [0] => Array ( [0] => Array ( [name] => date [value] => 2016-08-10 ) ) [1] => Array ( [0] => Array ( [name] => identity [value] => 11434058 ) ) [2] => Array ( [0] => Array ( [name] => company [value] => 1 ) ) [3] => Array ( [0] => Array ( [name] => cc [value] => 2 ) ) [4] => Array ( [0] => Array ( [name] => value [value] => 6 ) ) [5] => Array ( [0] => Array ( [name] => qov [value] => 1 ) ) [6] => Array ( [0] => Array ( [name] => concept [value] => 3 ) ) [7] => Array ( [0] => Array ( [name] => land [value] => 1 ) ) )

我需要将此数组保存在数据库中,但我无法做到。

Suppose I have a table like this:

<table id="parameters" width="100%" border="1">
  <thead>
    <tr>
      <th>Date</th>
      <th>Identity</th>
      <th>Names</th>
      <th>Quantity</th>
      <th>Concept</th>
      <th>Select</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>2016-08-10<input type="hidden" name="date[]" value="2016-08-10"></td>
      <td>12345678<input type="hidden" name="identity[]" value="12345678"></td>
      <td>Pepito Perez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>daytime<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check[]" value="12345678"></td>
    </tr>
    <tr>
      <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
      <td>24681012<input type="hidden" name="identity[]" value="24681012"></td>
      <td>Camilo Sanchez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>daytime<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check[]" value="24681012"></td>
    </tr>
    <tr>
      <td>2016-08-10<input type="hidden" name="fecha[]" value="2016-08-10"></td>
      <td>369121518<input type="hidden" name="identity[]" value="369121518"></td>
      <td>Pepito Perez</td>
      <td>2.5<input type="hidden" name="value[]" value="2.5"></td>
      <td>daytime<input type="hidden" name="concept[]" value="1"></td>
      <td><input type="checkbox" name="check" value="369121518"></td>
    </tr>
  </tbody>
  </table> 
  
 

This table is generated according to a query, the id of the checkbox is the identity value of each employee .

I want to get the value of the hidden input only from the checkbox selected in an array and then send that data by ajax and then save them in the database:

//Here, when clicking the button confirm searches the table that are marked confirm.on('click', function(){ //Save in result inputs with marked checkbox $("#parameters tbody tr td input[name=check]:checked").each(function(){ var result = []; $(this).closest('tr').find('input[type="hidden"]').each(function(){ result.push($(this).serializeArray()); }); //sending the object by ajax $.ajax({ type: "POST", url: getBaseUri()+'/payroll/createAll/', data: {datas: result}, cache: false, success: function (response) { var table = tableConfirm; var url = table.data('source'); clearFom(); table.dataTable().fnReloadAjax(url); }, error: function (response) { console.log(response); } }); });

I wonder what I'm doing wrong and if there is a better way to send the data by ajax to store them in the database because I receive the object like this:

$datas = Array ( [0] => Array ( [0] => Array ( [name] => date [value] => 2016-08-10 ) ) [1] => Array ( [0] => Array ( [name] => identity [value] => 11434058 ) ) [2] => Array ( [0] => Array ( [name] => company [value] => 1 ) ) [3] => Array ( [0] => Array ( [name] => cc [value] => 2 ) ) [4] => Array ( [0] => Array ( [name] => value [value] => 6 ) ) [5] => Array ( [0] => Array ( [name] => qov [value] => 1 ) ) [6] => Array ( [0] => Array ( [name] => concept [value] => 3 ) ) [7] => Array ( [0] => Array ( [name] => land [value] => 1 ) ) )

And I need to save this array in the database, but I can´t do it.

最满意答案

$("#parameters tbody tr td input[name=check]:checked").each(function(){ var result = []; $(this).closest('tr').find('input[type="hidden"]').each(function(){ result.push($(this).val()); });

是的我通过.val()更改了.val()并运行了。

$("#parameters tbody tr td input[name=check]:checked").each(function(){ var result = []; $(this).closest('tr').find('input[type="hidden"]').each(function(){ result.push($(this).val()); });

Yes I changed .serializeArrya() by .val() and it runs.

更多推荐

value,ajax,table,电脑培训,计算机培训,IT培训"/> <meta name="description&

本文发布于:2023-04-28 00:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:values   Javascript   hidden   table   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!