Jquery:如何在Jquery中获取Table单元格值

时间:2021-09-23 20:37:51
<table class="table" align="center" id="tblMain">
  <tr>
      <td>Status</td>
      <td><b><span name="current" value="Dispatch">Dispatch</span></b></td>
  </tr>
</table>

JS

$('#tblMain').find('td').ready(function() 

if( $('#current').val() == 'Dispatch')
{
    alert('Dispatch');
}
else
{
     alert('Nothing found');
}
);

I need table cell value to compare with with my status and should alert as given ..how to achieve this?

我需要表格单元格值与我的状态进行比较,并应该提醒..如何实现这一点?

2 个解决方案

#1


3  

  1. <span> don't have value attribute, so it is invalid HTML. Use custom data-* attributes.
  2. 没有value属性,因此它是无效的HTML。使用自定义data- *属性。

  3. current is the name of element, not id. Use attribute selector to select element by an attribute.
  4. current是元素的名称,而不是id。使用属性选择器按属性选择元素。

  5. ready should be called on document
  6. 准备应该在文件上调用

$(document).ready(function() {
  if ($('span[name="current"]').data('value') == 'Dispatch') {
    alert('Dispatch');
  } else {
    alert('Nothing found');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<span name="current" data-value="Dispatch">Dispatch</span>
<!--                 ^^^^^^^^^^^^^^^^^^^^^^            -->

#2


-1  

$(".table").find("tr td:nth-child(1)").text();

#1


3  

  1. <span> don't have value attribute, so it is invalid HTML. Use custom data-* attributes.
  2. 没有value属性,因此它是无效的HTML。使用自定义data- *属性。

  3. current is the name of element, not id. Use attribute selector to select element by an attribute.
  4. current是元素的名称,而不是id。使用属性选择器按属性选择元素。

  5. ready should be called on document
  6. 准备应该在文件上调用

$(document).ready(function() {
  if ($('span[name="current"]').data('value') == 'Dispatch') {
    alert('Dispatch');
  } else {
    alert('Nothing found');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<span name="current" data-value="Dispatch">Dispatch</span>
<!--                 ^^^^^^^^^^^^^^^^^^^^^^            -->

#2


-1  

$(".table").find("tr td:nth-child(1)").text();