Javascript 如何将每行的值推送到数组中?
我有可编辑的数据表,我想分别更新每一行。下面您可以看到我的js代码,我尝试将所选行推送到数组中。然后我会将其发送到php进行处理。但我无法选择要选择的指定行。数组返回空。我怎样才能解决这个问题 HTMLJavascript 如何将每行的值推送到数组中?,javascript,Javascript,我有可编辑的数据表,我想分别更新每一行。下面您可以看到我的js代码,我尝试将所选行推送到数组中。然后我会将其发送到php进行处理。但我无法选择要选择的指定行。数组返回空。我怎样才能解决这个问题 HTML 您的代码存在多个问题 有充分的理由将tr中的所有id更改为类 从html附加中的更新按钮删除onclick 更新js中的按钮侦听器 更改后请遵循以下代码: <tbody> <?php while($row = sqlsrv_fetch_array($resul
您的代码存在多个问题
<tbody>
<?php
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){ ?>
<form>
<tr>
<!-- EKN -->
<td class="id" style="display:none;"><?php echo $row['Id']?></td>
<td class="urunAciklama" class="pt-3-half" contenteditable="true"><?php echo $row['UrunAciklama']?></td>
<td class="miktar" class="pt-3-half" contenteditable="true"><?php echo $row['Miktar']?></td>
<td class="birimFiyati" class="pt-3-half" contenteditable="true"><?php echo $row['BirimFiyati']?></td>
<td class="satirTutari" class="pt-3-half" contenteditable="true"><?php echo $row['SatirTutari']?></td>
<td class="KdvOrani" class="pt-3-half" contenteditable="true"><?php echo $row['KdvOrani']?></td>
<td>
<span class="table-remove"><button type="button" class="updateBtn" class="btn btn-danger btn-rounded btn-sm my-0">Güncelle</button></span>
</td>
</tr>
</form>
<?php } //EoF While ?>
</tbody>
但是如果不循环整个表,代码就不起作用。
function updateInvoice(ID){
var faturaHareket=[];
$(function(){
$("click", function() {
faturaHareket = [];
$("table > tbody > tr").each(function () {
var $tr = $(this);
('#updateBtn').click(function () {
faturaHareket.push({
id: Number($tr.find("#id").text()),
urunAciklama: $tr.find("#urunAciklama").text(),
miktar: $tr.find("#miktar").text(),
birimFiyati: $tr.find("#birimFiyati").text(),
satirTutari: $tr.find("#satirTutari").text(),
kdvOrani: $tr.find("#kdvOrani").text()
});
});
});
console.clear();
console.log(JSON.stringify(faturaHareket));
});
});
<tbody>
<?php
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){ ?>
<form>
<tr>
<!-- EKN -->
<td class="id" style="display:none;"><?php echo $row['Id']?></td>
<td class="urunAciklama" class="pt-3-half" contenteditable="true"><?php echo $row['UrunAciklama']?></td>
<td class="miktar" class="pt-3-half" contenteditable="true"><?php echo $row['Miktar']?></td>
<td class="birimFiyati" class="pt-3-half" contenteditable="true"><?php echo $row['BirimFiyati']?></td>
<td class="satirTutari" class="pt-3-half" contenteditable="true"><?php echo $row['SatirTutari']?></td>
<td class="KdvOrani" class="pt-3-half" contenteditable="true"><?php echo $row['KdvOrani']?></td>
<td>
<span class="table-remove"><button type="button" class="updateBtn" class="btn btn-danger btn-rounded btn-sm my-0">Güncelle</button></span>
</td>
</tr>
</form>
<?php } //EoF While ?>
</tbody>
$(document).ready(function(){
$('.updateBtn').on('click', function(){
faturaHareket = [];
var $tr = $(this).closest('tr');
faturaHareket.push({
id: Number($tr.find(".id").text()),
urunAciklama: $tr.find(".urunAciklama").text(),
miktar: $tr.find(".miktar").text(),
birimFiyati: $tr.find(".birimFiyati").text(),
satirTutari: $tr.find(".satirTutari").text(),
kdvOrani: $tr.find(".kdvOrani").text()
});
})
})