2015-07-20 58 views
0
<table class="table" align="center" id="tblMain"> 
    <tr> 
     <td>Status</td> 
     <td><b><span name="current" value="Dispatch">Dispatch</span></b></td> 
    </tr> 
</table> 

JSjQuery的:如何在jQuery中

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

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

我需要的表格單元格的值與我的狀態比較並應提醒給出..how實現這一目標得到表單元格的值?

+0

被稱爲'span'沒有'value'屬性。無效的HTML; – Tushar

+0

請更正您的HTML示例。您沒有關閉''而不是關閉'

回答

3
  1. <span>沒有value屬性,所以它是無效的HTML。使用自定義data-*屬性。
  2. current是元件的name,而不是id。使用attribute selector按屬性選擇元素。
  3. ready應該document

$(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> 
 
<!--     ^^^^^^^^^^^^^^^^^^^^^^   -->

+0

'#current' !!! ??? –

+0

@TrueBlueAussie請檢查更新後的答案 – Tushar

+0

好得多:)'data-'usage是個不錯的建議。注意:原始的HTML也是「只是簡單的破碎」(不結束'',未封閉的粗體) –

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

沒有'$ .trim'?由於內容不斷變化,內容的文本比較不可靠,但是當原始文件嘗試使用自定義屬性時,這種情況尤其不對。將其改爲「數據」屬性將是正確的解決方案。根據@Tushar。另外,當name屬性存在時使用'nnth-child(1)'只是使它比所需的更加複雜。 –