【问题标题】:Javascript function response in Jquery not workingJquery中的Javascript函数响应不起作用
【发布时间】:2013-01-22 23:19:45
【问题描述】:

我有一个表单,它使用 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...

获取“文本...”没有问题,但无法在视频页面上实时显示“”。

请帮忙...

【问题讨论】:

  • 不应该是$(#comment)$('#comment')
  • @Jay Blanchard 链接已损坏。 :(
  • 你怎么知道脚本没有运行? &lt;script&gt; 块中究竟是什么?
  • 您说服务器的响应是 JSON,但您的 PHP 脚本中的 echo 部分看起来不像是 JSON。您发布的代码非常混乱。
  • 这和jQuery无关,你必须在服务器上创建JSONecho "&lt;script&gt;...&lt;/script&gt;"; 当然不会输出 JSON,而是输出部分 HTML。这也很好,但是不要告诉 jQuery 期待 JSON,response 将是一个包含 HTML 的字符串。字符串没有code 属性(我想知道你为什么认为它应该存在)。

标签: php javascript jquery ajax


【解决方案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");

});

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

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

【讨论】:

  • 我不能将 javascript 代码放在 post 响应中,因为 javascript 函数显示在文本中。例如:文本... javascript ...文本...
  • @richard 我明白了,你能分享一下php里面的js吗?你有onLoad 电话吗?
  • @tq,我已经更新了我的问题中的 javascript。这是一个jw播放器。
  • 我刚试过你的onload解决方案,但问题是,它只是显示“text...
    text...”,里面的一切 丢失。
  • 现在看起来你有response.msg,这意味着响应正在寻找msg,你可以只使用response吗?
猜你喜欢
  • 1970-01-01
  • 2021-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 2020-09-05
相关资源
最近更新 更多