0

我有一些 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>";
?>

认为我剪掉了所有与我的问题无关的绒毛。

4

0 回答 0