2015-06-26 75 views
0

我想讓我的表只用於Home_Score和Away_Score名稱的列可編輯如何將jquery onclick函數設置爲僅定位可編輯表格中的特定表格列或類?

//edit.js文件

$(function() { 

    //when a td element within tbody is clicked 
    $('tbody').on('click','td',function() { 
     //call displayform, passing td jQuery element 
     displayForm($(this)); 
    }); 

}); 

//table.php文件

<?php 

        $sql = "SELECT * FROM predict WHERE Fixture_ID BETWEEN '1' and '10' "; 
        $results = $dbh->query($sql); 
        $rows = $results->fetchAll(); 

        foreach ($rows as $row) { 
         echo '<tr id="'.$row['Fixture_ID'].'">'; 
         echo '<td class="Home_Team">'.$row['Home_Team'].'</td><td class="Home_Score">'.$row['Home_Score'].'</td>'; 
         echo '<td class="Away_Score">'.$row['Away_Score'].'</td><td class="Away_Team">'.$row['Away_Team'].'</td>'; 
         echo '</tr>'; 
        } 
       ?> 
+0

開始'元素代替主頁和離開文本。 – hoss

回答

0

我相信你可以做到這一點

$(function() { 
    //when a td element within tbody is clicked 
    $('tbody').on('click', 'td.Home_Score, td.Away_Score', function() { 
     //call displayform, passing td jQuery element 
     displayForm($(this)); 
    }); 
}); 

另外,作爲在另一個答案中建議,將另一個類添加到要編輯的單元格中,並將其用作選擇器。

+0

謝謝。這工作完美。 –

1

假設你使用`

`和` 被添加行,你可以添加一個類只是行你把瓦萊斯

<?php 

       $sql = "SELECT * FROM predict WHERE Fixture_ID BETWEEN '1' and '10' "; 
       $results = $dbh->query($sql); 
       $rows = $results->fetchAll(); 

       foreach ($rows as $row) { 
        echo '<tr id="'.$row['Fixture_ID'].'">'; 
        echo '<td class="Home_Team">'.$row['Home_Team'].'</td><td class="Home_Score editable">'.$row['Home_Score'].'</td>'; 
        echo '<td class="Away_Score editable">'.$row['Away_Score'].'</td><td class="Away_Team">'.$row['Away_Team'].'</td>'; 
        echo '</tr>'; 
       } 
      ?> 

那麼你可以更新您的選擇

$('tbody').on('click','.editable',function() { 
+0

Upvote for reusable solution。 – vcanales

相關問題