繁体   English   中英

jQuery脚本在Firefox中有效,在Chrome中停止

[英]jQuery script works in Firefox, stops in Chrome

脚本应发布评论,并在从服务器加载答案时发布。 它可以在Firefox中运行,但是在Chrome中,我想没有事件被触发,您可以单击一个按钮,但是什么也不会发生。 我已经在Chrome开发者工具中检查了一些错误,但没有发现任何错误。

HTML:

<div class="posted_comments" id="comments'.$post_id.'"></div>
<form method="post" id="c'.$post_id.'">
    <input class="comment_input" placeholder="Write your comment here..." name="comment" />
    <input name="post_id" id="post_id" hidden="hidden" value='.$post_id.' >
    <button class="comment_button" onclick="post_comment('.$post_id.')">Send</button>
</form>

jQuery脚本:

 function post_comment(id) {
     x = "#c" + id;
     y = "#comments" + id;
     $(x).submit(function () {
         $.ajax({
             type: 'POST',
             url: 'post_comment.php',
             data: $(x).serialize(),
             cache: false,

             success: function (data) {
                 $(y).html(data);
             },

             complete: function () {
                 $(x)[0].reset();
                 $(x).unbind();
             }
         });
         return false;
     });
 };

不要只是在开发人员工具中查找错误。 使用Chrome调试器在代码中设置断点,然后查看断点。

您可以添加debugger;也可以添加debugger; 声明到您要停止的代码,或者在Chrome调试器的“源”标签中打开代码,然后单击左侧空白以设置断点。

无论哪种方式, post_comment()post_comment()函数的开头, submit()回调的开头(即$.ajax()行)以及每个successcomplete回调的开始处设置断点。

然后,您可以查看代码得到的距离,以及变量等信息。 这应该为您提供更多线索。

这是JavaScript调试简介,以及有关Chrome DevTools的更多信息

我敢肯定,Chrome会在不执行您的功能的情况下提交表单,因此您需要阻止默认操作。

<button class="comment_button" data-id="' . $post_id . '">Send</button>

$('.comment_button').click(function(event){
   event.preventDefault();
   //put you ajax here, and get the post ID with $(this).attr('data-id');
});

尝试这个

HTML:

<php include ('data.php');?>
    <form action="action.php" method="post">
    <label>Name:</label>
    <input name="name" type="text" placeholder="Name here"/><br>
    <label>Comment:</label>
    <textarea name="comment" type="text" placeholder="Write a comment here"/></textarea>
    <br>
    <input type="submit" name="submit" value="Send">

然后创建一个动作文件

action.php

<?php 
$handle = fopen("data.php", "a");
if( isset( $_POST['name'] ) &&  strlen( $_POST['name'] ))
    {
fwrite($handle,"<br>");
fwrite($handle,'---------------------------------------');
fwrite($handle,"<br><b>");
fwrite($handle,$_POST["name"]);
fwrite($handle,'<br> Comment: <br>');
fwrite($handle,$_POST["comment"]);
fclose($handle); 
header("Location: ./index.php");
       }
else
     {
echo "name required <br> <a href='./index.php'>OK</a>";
      }
exit;
?>

如果不起作用,则需要编辑一些代码

根据 :

<div class="posted_comments" id="comments'.$post_id.'"></div>
<form method="post" id="c'.$post_id.'">

和:

x = "#c" + id;
y = "#comments" + id;

如果您这样称呼:

post_comment('.$post_id.')

那么您的变量将是:

x = "#c.$post_id.";
y = "#comments.$post_id.";

在这种情况下,您的ID是错误的,您将使用额外的单引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM