douchun9719 2013-03-06 21:05
浏览 31

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

I have some jQuery that handles a click event on a table row,which is built with PHP, when it is clicked. It works fine as long as the table is built in the same file. It however will not work if I have jQuery go out to a PHP file, have the PHP build the table, then send the output back to be written in a div tag. I have tried adding .live to the event but that didn't work either? Any suggestions?

The html and 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>

And the php called by buildMsgTable:

<?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>
";
            }
        }
        $errorlist .= "</ol></li>
";
        $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>";
?>

Think I cut out all the fluff that doesn't pertain to my question.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序