简体   繁体   中英

jQuery blur event not fired for table cell

I'm loading a table dynamically using jQuery ajax, the rows of the table has "contenteditable=true", I'm trying to listen on blur event for every cell, so that it triggers a function to update that cell dynamically. The problem is that the event blur isn't fired at all, I've tried different selectors(table,tbody, and finally the whole document), but all in vain.

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

try

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

seems that table td isn't a standard focusable element so you can't blur it. try to add the tabsindex property to each td

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

the definition of bind function is as follows:

.bind( eventType [, eventData ], handler )

so, you should do:

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

And as mentioned by @paul roub in the comments above, you should be using live() function, as you are creating the td elements dynamically.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM