0

我有一个使用 ajax 将评论发布到 video_comment.php 的表单,然后在视频页面上实时显示评论。

这是我的jQuery代码:

$("input[id*='post_video_playallcomment_']").livequery('click', function(event) {
    event.preventDefault();    
    var video_msg   = $("#post_message");
    var input_id    = $(this).attr('id');
    var id_split    = input_id.split('_');
    var video_id    = id_split[3];                    
    var comment     = $("textarea[id='video_comment']").val();
    var response_messages = '#video_response';        

    if ( comment == '' ) {
        video_msg.fadeIn();
        return false;
    }

    video_msg.hide();
    user_posting(response_messages, '<?php echo $lang['600']; ?>', 1);
    reset_chars_counter();
    $.post(base_url + '/video_comment.php', { video_id: video_id, comment: comment },
    function(response) {
        if ( response.msg != '' ) {
            $(response_messages).hide();
            user_posting(response_messages, response.msg);                    
            $("textarea[id='video_comment']").val('');
        } else {
            $(response_messages).hide();
            user_response(response_messages, '<?php echo $lang['587']; ?>');             
            $(".no_comments").hide();
            $("textarea[id='video_comment']").val('');
            var bDIV = $("#comments_delimiter");
            var cDIV = document.createElement("div");
            $(cDIV).html(response.code);
            $(bDIV).after(cDIV);
        }
    }, "json");
});                         

在 video_comment.php 上,这是在视频页面上实时显示评论的 response.code。

echo 'text...

echo '<div id="commentplayer"></div>';
echo '<script type="text/javascript">';
echo 'jwplayer("commentplayer").setup({';
echo 'file: "...url",';  
echo 'width: "300",';
echo 'height: "225",';
echo 'provider: "http",';
echo 'startparam: "start",';
echo 'wmode: "transparent",';        
echo 'controls: "true",'; 
echo 'stretching: "uniform",';    
echo 'primary: "flash",';       
echo 'autostart: "false",';
echo 'ga: {}';
echo '});';
echo '</script>';

echo 'text...

我可以毫无问题地获取“文本...”,但我无法让“<script type="text/javascript">function...</script>”实时显示在视频页面上。

请帮忙...

4

1 回答 1

1

而不是将javascript代码放在php返回中,为什么不将它放在.post响应中?

$("input[id*='post_video_comment']").click(function() {           
$.post('video_comment.php', function(response) {
  $('#comment').html(response.code);  
    //place javascript code here
}, "json");

});

onload或在您的js代码之前放置一个

$str = '<div id="commentplayer"></div>
<script type="text/javascript">
window.onload=(function() {
    jwplayer("commentplayer").setup({
    file: "...url",
    width: "300",
    height: "225",
    provider: "http",
    startparam: "start",
    wmode: "transparent",      
    controls: "true", 
    stretching: "uniform",  
    primary: "flash",     
    autostart: "false",
    ga: {}
    });
});
</script>';

echo $str;
于 2013-01-22T14:35:28.087 回答