使用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();   
  }
}