检查表中值的条件

时间:2014-10-30 13:55:07

标签: jquery

我有这个表格标签html

<table class="table  dataTable" id="customer_details_table" aria-describedby="sample_1_info" style="display: table;">
   <thead>
      <tr>
         <th></th>
         <th>Customer Name</th>
         <th>Address</th>
         <th>Contact No.</th>
         <th>Email</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody class="odd gradeX">
      <tr>
         <td><label class="radio"><input type="radio" onclick="UpdateButtonTest(this);" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">7654321987</td>
         <td width="20%">venkatrajkiran@yahoo.com</td>
         <td width="10%" align="center"><span class="label label-success">Active</span></td>
      </tr>
      <tr>
         <td><label class="radio"><input type="radio" onclick="UpdateButtonTest(this);" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">9701429843</td>
         <td width="20%">s@g.com</td>
         <td width="10%" align="center"><span class="label label-inverse">DeActive</span></td>
      </tr>
   </tbody>
</table>

我需要找出表格中是否有动作值

中名称DeActive的任何值

我试过这种方式

  function find() {

        var header = document.getElementById("customer_details_table").getElementsByTagName("th");
        for (var j = 0; j < header.length; j++) {
        if (header[j].innerHTML == "DeActive") 
          alert("Found");
        }

}

有人可以告诉我如何找出任何行动是否具有DeActive的价值?

http://jsfiddle.net/kz2j2jjy/1/

5 个答案:

答案 0 :(得分:2)

只需遍历每个td并进行比较:

fucntion find(){
     $("#customer_details_table tr td span").each(function(){
         var texttocheck = $(this).html();
         if(texttocheck == "DeActive"){
         alert("found");
         }
     });
}

答案 1 :(得分:1)

如果你正在使用jQuery,那很简单:

var tds = $("table#customer_details_table tr td:contains('DeActive')");

答案 2 :(得分:1)

你标记了jQuery,但是既然你发布了纯JavaScript,那么这是一个JS解决方案:

function find() {
  var tb= document.getElementById('customer_details_table'),
      header = tb.getElementsByTagName('th'),
      cell,
      j,
      k;
  
  for(j = 0; j < header.length; j++) {
    if (header[j].innerText === 'Action') {
      for(k = 1 ; k < tb.rows.length ; k++) {
        cell = tb.rows[k].cells[j]
        if(cell.innerText==='DeActive') {
          cell.style.background= 'orange';
        }
      }
    }
  }
}

find();
<table class="table  dataTable" id="customer_details_table" aria-describedby="sample_1_info" style="display: table;">
   <thead>
      <tr>
         <th></th>
         <th>Customer Name</th>
         <th>Address</th>
         <th>Contact No.</th>
         <th>Email</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody class="odd gradeX">
      <tr>
         <td><label class="radio"><input type="radio" onclick="UpdateButtonTest(this);" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">7654321987</td>
         <td width="20%">venkatrajkiran@yahoo.com</td>
         <td width="10%" align="center"><span class="label label-success">Active</span></td>
      </tr>
      <tr>
         <td><label class="radio"><input type="radio" onclick="UpdateButtonTest(this);" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">9701429843</td>
         <td width="20%">s@g.com</td>
         <td width="10%" align="center"><span class="label label-inverse">DeActive</span></td>
      </tr>
   </tbody>
</table>

答案 3 :(得分:0)

首先你应该使用jQuery,它会让你的生活更轻松。

看看这个小提琴:

http://jsfiddle.net/kz2j2jjy/3/

浏览器准备就绪后,将调用$()内的函数。 each()循环只迭代所有行,搜索span.label - 元素并检查元素的内容。如果内容为DeActive,则循环中断(return false;)并发出警报。

此外,您的JSFiddle中出现了一个小错误:find()函数从未被调用过。

答案 4 :(得分:0)

$('#customer_details_table').find('tr:has(td:last-child:contains("DeActive"))').each(function() {
  var index = $(this).closest('table').find('tr:has(td)').index( this ) + 1;
  alert( 'found in row: ' + index );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table  dataTable" id="customer_details_table" aria-describedby="sample_1_info" style="display: table;">
   <thead>
      <tr>
         <th></th>
         <th>Customer Name</th>
         <th>Address</th>
         <th>Contact No.</th>
         <th>Email</th>
         <th>Action</th>
      </tr>
   </thead>
   <tbody class="odd gradeX">
      <tr>
         <td><label class="radio"><input type="radio" onclick="UpdateButtonTest(this);" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">7654321987</td>
         <td width="20%">venkatrajkiran@yahoo.com</td>
         <td width="10%" align="center"><span class="label label-success">Active</span></td>
      </tr>
      <tr>
         <td><label class="radio"><input type="radio" onclick="UpdateButtonTest(this);" name="optionsRadios1" value="option1"></label></td>
         <td width="20%">kiran</td>
         <td width="25%">XXXXXXX</td>
         <td width="20%">9701429843</td>
         <td width="20%">s@g.com</td>
         <td width="10%" align="center"><span class="label label-inverse">DeActive</span></td>
      </tr>
   </tbody>
</table>