簡體   English   中英

表格單元格未觸發jQuery模糊事件

[英]jQuery blur event not fired for table cell

我正在使用jQuery ajax動態加載表,表的行具有“ contenteditable = true”,我試圖監聽每個單元格的模糊事件,以便它觸發一個函數來動態更新該單元格。 問題是事件模糊根本沒有觸發,我嘗試了不同的選擇器(表,正文,最后是整個文檔),但都沒有用。

<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>

嘗試

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

表td似乎不是標准的可聚焦元素,因此您無法對其進行模糊處理。 嘗試將tabsindex屬性添加到每個td

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

綁定函數的定義如下:

.bind( eventType [, eventData ], handler )

因此,您應該這樣做:

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

正如@paul roub在上面的評論中提到的那樣,您應該使用live()函數,因為您正在動態創建td元素。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM