使用jQuery比较表中不同行中单元格的值 [英] compare values of cells in different rows in table using jquery

查看:133
本文介绍了使用jQuery比较表中不同行中单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的带有php的表,该表具有相同的行.需要从第1行的单元格1中获取值,并从第2行的单元格1中获取值,然后将它们进行比较.如果它们是相同的,则删除整行或隐藏...对整个表执行此操作...任何帮助都将不胜感激..谢谢!

I have a dynamically generated table with php that has same rows. Need to get value from cell 1 in row 1 and value from cell 1 in row 2 and compare them. If they are the same remove entire row or hide... Do that for the whole table... Any help is appreciated.. Thanks!!

目前为止:

var numRows = $('table#changeSubjectKatedra tr').lenght;
var i = 0;
do {
    var cur = $('input#'+i).val();
    var next = $('input#'+(i+1)).val();
    if(cur == next){
        $('tr#'+i).remove();
        }
    i++;
} while(i<numRows);

表中的行如下所示:

<tr id='idNum'><td>someText<input type='hidden' value='someData' id='idNum'>
</td><td>someText</td></tr>  

推荐答案

注1.您应该在服务器端使用PHP而不是JavaScript.

Note 1. You should do in on server side with PHP, not with JavaScript.

注意2.您必须为每个元素使用唯一的ID.

Note 2. You must use unique id for each element.

注意3.避免使用元素的数字ID.

Note 3. Avoid using numerical ids of elements.

注意4.做您想做的事情根本不需要ID.

Note 4. You don't need ids at all for doing what you want.

如果您仍想使用JavaScript进行操作,建议您采用以下方式:( 此处为在线演示)

If you still want to do it in JavaScript, I suggest you to do it this way: (live demo here)

var rows = $("#changeSubjectKatedra tr");
for(var i = 0; i <= rows.length - 2; i++) {
  if ($(rows[i]).find("input").val() ==
      $(rows[i+1]).find("input").val()) {
    $(rows[i]).remove();   
  }
}

这篇关于使用jQuery比较表中不同行中单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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