Ajax根据行获取表值 [英] Ajax get table value based on row

查看:126
本文介绍了Ajax根据行获取表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从我的行中获取第一个和最后一个值,但无法获得我的行的第二个和第三个值。谁能帮我。



这是我的代码

=> Html

 < TR> 
< td> one< / td>
< td> two< / td>
< td>三< / td>
< td>四< / td>
< td>< button class =btnDelete>删除< /按钮>< / td>
< / tr>

=> javascript

  $(。btnDelete)。click(function(evt){
var cell = $(evt.target).closest(tr)。children()。first();
var cell2 = $(evt.target).closest(tr)。children()。last();
var custID = cell.text();
var custID2 = cell2 .text();
alert(custID);
alert(custID2);
}

感谢。

解决方案

我认为在没有jQuery的情况下获取这个值会更容易,通过使用 HTMLTableRowElement.cells DOM属性。它几乎就像一个数组,但不是一个数组。


$ b $

  $(#myTable)。on('click','。btnDelete',function(){
//获取当前行
var currentRow = $(this).closest(tr)[0];
var cells = currentRow.cells;

var firstCell = cells [0] .textContent;
var secondCell = cells [1] .textContent;

// ...
// nthCell = cells [n-1] .textContent;
console.log(firstCell);
console.log(secondCell);
});

如果您仍然需要jQuery,那么不要使用 .first() code>和 .last()方法,您可以使用 .eq() 方法。

  var rowCells = $(this).closest(tr)。children(); 
var firstCell = rowCells.eq(0).text();
var secondCell = rowCells.eq(1).text();


I can get first and last value from my row but can`t get the second and third value of my row. can anyone help me.

this my code

=> Html

<tr>
    <td>one</td>
    <td>two</td>
    <td>three</td>
    <td>four</td>
    <td><button class="btnDelete">Delete</button></td>  
</tr>

=> javascript

$(".btnDelete").click(function (evt) {
   var cell=$(evt.target).closest("tr").children().first();
   var cell2=$(evt.target).closest("tr").children().last();
   var custID=cell.text();
   var custID2=cell2.text();
   alert(custID);
   alert(custID2);
}

thanks .

解决方案

I think it's easier to get this values without jQuery. By using HTMLTableRowElement.cells DOM property. Which is almost like an array, but not an array.

$("#myTable").on('click','.btnDelete',function(){
     // get the current row
     var currentRow = $(this).closest("tr")[0]; 
     var cells = currentRow.cells;

     var firstCell = cells[0].textContent;
     var secondCell = cells[1].textContent;

     //...
     //nthCell = cells[n-1].textContent;
     console.log( firstCell );
     console.log( secondCell );
});

If you still want jQuery, then instead of .first() and .last() methods, you could use .eq() method.

 var rowCells = $(this).closest("tr").children(); 
 var firstCell = rowCells.eq( 0 ).text();
 var secondCell = rowCells.eq( 1 ).text();

这篇关于Ajax根据行获取表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆