2015-11-04 26 views
0

我正在使用jQuery ajax動態地加載表格,表格的行有「contenteditable = true」,我正在嘗試爲每個單元格模糊事件進行偵聽,以便它觸發一個函數動態更新該單元格。 問題是,事件模糊並沒有被解僱,我嘗試了不同的選擇器(表,tbody,最後是整個文檔),但都是徒勞的。jQuery模糊事件沒有解僱表格單元格

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title></title> 
     <script type="text/javascript" src='jquery-1.8.3.js'></script> 
    <link rel="stylesheet" href='jquery-ui-1.8.7.custom.css' type="text/css"> 
     <?php 
     include './datatable_include.php'; 
     ?> 
     <script type="text/javascript"> 

      $(function() { 
       $.ajax({//create an ajax request to load_page.php 
        type: "GET", 
        url: "load_table.php", 
        dataType: "html", //expect html to be returned     
        success: function (response) { 
         $('#dataTable').find('tbody').html(response); 
         initDataTable('#dataTable', null); 
        } 
       }); 
      }); 


      $(document).bind("blur", "td", (function() { 
       // this code isn't reached 
       alert("ahoo"); 

       var id = $(this).attr("id"); 
       var name = $(this).attr("name"); 
       var message_status = $("#status"); 
       var value = $(this).text(); 
       $.post('update_table.php', "id=" + id + "&" + name + "=" + value, function (data) { 
        if (data != '') 
        { 
         message_status.show(); 
         message_status.text(data); 
         //hide the message 
         setTimeout(function() { 
          message_status.hide() 
         }, 3000); 
        } 
       }); 


      })); 


     </script> 
    </head> 
    <body> 
     <table id="dataTable" width="700px" > 
      <thead> 
       <tr>  
        <th>Name</th> 
        <th>ID</th> 
       </tr> 
      </thead> 
      <tbody> 
      </tbody> 
     </table> 
    </body> 
</html> 
+0

你嘗試'$(文件)。在(「模糊」 ...' – caramba

+0

不幸的是,我必須使用「綁定」,而不是「上」,因爲的頁面使用過時的jQuery版本 – AbdulRahman

+0

ooh我甚至沒有看過那個,你多次包括jQuery。那就不好了!爲了你的不幸,我沒有辦法解決那麼 – caramba

回答

0

嘗試

$(document).bind("td").blur(function() 
{ 
}); 
+0

也沒有工作 – AbdulRahman

-1

bind函數的定義如下:

.bind(eventType [, eventData ], handler) 

所以,你應該做的:

$('td').bind('blur', function(){ 
    //event handler statement goes here 
}); 

以及由@保羅提到在上面的評論中,你應該使用roub live()函數,因爲您正在動態創建td元素。

$('td').live('blur', function(){ 
//event handler statement goes here 
}); 
+0

'.live()'很久以前不再使用:http://api.jquery.com/live/並且'.bind()'已過時。你應該使用'.on()':http://api.jquery.com/on/ –

+0

知道。但正如在問題的評論中提到的那樣,頁面上使用的jquery版本已經過時了。 –

+0

我更新了上面的代碼,並刪除了古代版本的jQuery,不幸的是使用on()或不推薦使用的bind()也不起作用。 – AbdulRahman

0

似乎表td不是一個標準的可重點元素,所以你不能模糊它。 嘗試將tabsindex屬性添加到每個TD

<td tabindex="1">focus on me</td>