2013-07-30 67 views
0

如何使用javascript檢查表中的重複行?以下是我的代碼部分:如何使用javascript檢查重複行

<table id="t1"> 
<tr> 
    <td>Text A</td> 
    <td>Text B</td> 
    <td>Cbx A</td> 

</tr> 

<% int count1 = -1; 
    for(int i=0; i<3; i++) { %> 
    <tr> 
    <td><input type="text" id="textA<%=i%>"></td> 
    <td><input type="text" id="textB<%=i%>"></td> 
    <td><select name="cbx_A<%=i%>"> 
      <option value="A">Option1</option> 
      <option value="B">Option2</option> 
     </select> 
    </td 
    </tr> 
<%count1 =i; 
    }%> 
    <tr> 
    <td><input type="button" onclick="check(<%=count1%>)" value="Check"></td> 
    </tr> 
</table> 

因此,基於此代碼,我將有3行文字A,TEXTB和cbxA的。那麼,我該如何檢查用戶是輸入2行還是全3行的相同值? 我嘗試過使用servlet,但涉及太多的工作。那麼是有沒有辦法使用java腳本來做到這一點呢?

在此先感謝您的幫助。

回答

0

使用此代碼將在一個表中的列檢查重複 然後採取被複製表的所有行,並把它們的ID在陣列 所以你會得到行ID 的陣列,但烏爾表都必須有一個每行id爲

var columnNumber = 1; 
var table = document.getElementById('t1'); 
var rowLength = table.rows.length; 
var arrReocrds = new Array(); 
var arrCount = 0; 
var listCount = 0; 
var arr = new Array(); 
$('#t1 td:nth-child(' + colNumber + ')').each(function() { 
      var recordValue = $(this).html().trim(); 
      var flagFirstTime = true; 
      //loop through table to check redundant of current record value 
      for (var i = 1; i < rowLength; i += 1) { 
       { 
        var row = table.rows[i]; 
        var recordId = //put here row.id or anything that you can put it in the list 
        var cell = row.cells[colNumber - 1]; 
        var value = cell.innerText.trim(); 
        if (value == recordValue) { 
         if (!arr.contains(value)) { 
          if (flagFirstTime != true) { 
           arrReocrds[arrCount] = recordId; 
           arrCount++; 
          } 
          else 
           flagFirstTime = false; 
         } 
         else 
          break; 
        } 
       } 
      } 
      //create list for items in column 
      //to be sure that the item that has been taken and checked never be checked again by their other redundant records 
      arr[listCount] = recordValue; 
      listCount++; 
     });