2014-10-11 23 views
0

我有很多的表,在這我要做到以下幾點,jQuery的發現複選框中第一款TD

  1. 找到一個表,它存在於類。
  2. 發現第一TR,第一個TD表中的
  3. 檢查複選框,目前TD首先在表格
  4. 如果出現在第一款TD複選框,然後添加類。

下面是我的代碼是不是.length物業工作

function myFunction() { 
    debugger; 
    var FindClass = $("table.Panel"); 
    debugger; 
    var FindClass = $(".Panel table.Table"); 
    debugger; 
    debugger; 
    if (FindClass != null) { 
     $("#FindClass tr").find("td:first").tagname("input"); 

    } 
} 
+1

什麼是'tagname()'? – 2014-10-11 11:40:04

回答

0

我們可以在2爲此在2分簡單的方法實現這一目標...

    1. 查找與類選擇表。通過有條件檢查,我們可以將該類添加到複選框。
    1. 在不執行條件操作的情況下在一行中實現完整的代碼。

HTML

<table class="Panel"> 
    <tr> 
     <td><input type="checkbox" /></td> 
     <td><p>Test</p></td> 
    </tr> 
    <tr> 
     <td>Second TD</td> 
    </tr> 
</table> 

jQuery的(第一方法)

if($('table.Panel').length > 0) { 
    var tblCheckbox = $('table.Panel tr:first td:first input[type=checkbox]'); 
    if(tblCheckbox.length > 0) { 
     tblCheckbox.addClass('clstochkbox'); 
    } 
} 

jQuery的(第一方法)

$('table.Panel tr:first td:first input[type=checkbox]').addClass('clstochkbox'); 

http://jsfiddle.net/64jv3z6d/

0

檢查如jQuery的對象是從未null。並將其命名爲不同。

var panelTable = $(".Panel table.Table"); 
if (panelTable.length) { 
    // panelTable has elements 
} 
0

你可以這樣做

var chk_box = $("table.Panel tr:first td:first") 
             .find('input type=["checkbox"]'); 

if(chk_box.length) { 
    $(chk_box.addClass('x') 
} 
0

我們可以做到這一點也是這樣。

<script type="text/javascript"> 
function myFunction() { 
    debugger; 
    var headerRow = $("table.Panel tr:first th:first"); 
    debugger; 
    if (headerRow != null) { 
     var checkbox = headerRow.find("input[type=checkbox]"); 
     if (checkbox[0].type == 'checkbox') { 
      headerRow.addClass('checkboxColumns'); 

      alert('checkbox Found') 
     } else { 
      alert('not found') 
     } 
    } 

} 

</script>