假设我有一个这样的表:
<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&
发布评论