动态构建表的Click事件不会触发

时间:2013-03-06 21:05:47

标签: php jquery events html-table click

我有一些jQuery可以处理表格行上的点击事件,该表格是在点击时使用PHP构建的。只要表在同一文件中构建,它就可以正常工作。然而,如果我让jQuery转到PHP文件,让PHP构建表,然后将输出发送回div标签中,它将无法工作。我曾尝试将.live添加到活动中,但这也不起作用?有什么建议吗?

html和jQuery:

<html>
    <head>
        <script type='text/javascript' src='jquery.js'></script>
        <script type='text/javascript'>

        var name;
        var text;
        var subject;
        var id;
        var key;

        $(document).ready(function(){

            buildMsgTable();

            //Load Message Data in click
            $('#messages tr').click(function(){

                $(this, 'tr').each(function(index, tr) {

                    var lines = $('td', tr).map(function(index, td) {
                        return $(td).text();
                    });

                    key = lines[0];

                    name = lines[1];

                    textarea = lines[2];

                    subject = lines[3];

                    //alert(textarea);

                    $('#name').val(name);
                    $('#subject').val(subject);
                    $('#body').val(textarea); 

                })
            });

        function buildMsgTable(){
            $.post('/php_webfiles/edit/buildMsgTable.php',
                      {},
                      function(output){ $('#existingMessagesDiv').html(output); });
        }
        </script>
    </head>
</body>
<div id='existingMessagesDiv'></div>
<div id = 'debug'></div>
</body>
</html>

由buildMsgTable调用的php:

<?php
include "hawkfunctions.php";

$tsql = "SELECT * FROM Messages";

$conn  = mssqlConnect();
$stmt = sqlsrv_query($conn, $tsql);
//$stmt2 = sqlsrv_query($conn, $tsql2);

if ($stmt1 === false) {
    echo "Error in query preparation/execution (contacts_in_list).<br/>$tsql1<br/>";
    //die( print_r( sqlsrv_errors(), true));

    echo "Errors while connecing attempting to run query:<br/><ol>";
    $i = 1;

    foreach (sqlsrv_errors() as $masterError) {
        $errorlist .= "<li>Heading $i<ol>";

        foreach ($masterError as $root => $error) {
            if (!is_numeric($root)) {
                $errorlist .= "<li><b>$root:</b> $error</li>\n";
            }
        }
        $errorlist .= "</ol></li>\n";
        $i++;
    }
    $errorlist .= "</ol>";
    die($errorlist);
}

echo "<table border='1' id= 'messages'><th>Key</th><th>Name</th><th>Text</th><th>Subject</th>";


while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
    echo "<tr><td class='key'>";
    echo $row['MessageKey'];
    echo "</td>";
    echo "<td class = 'name'>";
    echo $row['Name'];
    echo "</td><td class = 'text'>";
    echo $row['Text'];
    echo "</td><td class = 'subject'>";
    echo $row['Subject'];
    echo "</td>";
    echo "<td class = 'delete'>";
    echo "<input type='button' value='Delete' id='delete' onclick='deleteMessage(".$row['MessageKey'].")' />";
    echo "</td>";
    echo "</tr>";
}

/* Free statement and connection resources.*/
sqlsrv_free_stmt( $stmt);
sqlsrv_close($conn);

echo "</table>";
?>

认为我删除了与我的问题无关的所有绒毛。

0 个答案:

没有答案