Javascript jQuery脚本在Firefox中工作,在Chrome中停止
脚本应该在从服务器加载答案时发布注释和提示。它在Firefox中工作,但在Chrome中我想不会触发任何事件,你可以点击一个按钮,但什么也不会发生。我已经在Chrome开发者工具中检查了一些错误,但没有发现任何错误 HTML:Javascript jQuery脚本在Firefox中工作,在Chrome中停止,javascript,jquery,html,google-chrome,Javascript,Jquery,Html,Google Chrome,脚本应该在从服务器加载答案时发布注释和提示。它在Firefox中工作,但在Chrome中我想不会触发任何事件,你可以点击一个按钮,但什么也不会发生。我已经在Chrome开发者工具中检查了一些错误,但没有发现任何错误 HTML: <div class="posted_comments" id="comments'.$post_id.'"></div> <form method="post" id="c'.$post_id.'"> <input cl
<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>
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提交表单时没有执行您的函数,所以您需要防止默认操作
<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');
});
发送
$('.comment_按钮')。单击(函数(事件){
event.preventDefault();
//将ajax放在这里,并使用$(this.attr('data-ID')获取post ID;
});
我确信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');
});
发送
$('.comment_按钮')。单击(函数(事件){
event.preventDefault();
//将ajax放在这里,并使用$(this.attr('data-ID')获取post ID;
});
不要只查看开发人员工具中的错误。使用Chrome调试器在代码中设置断点,并查看它可以到达哪些断点
您可以添加调试器将代码>语句添加到要停止的代码中,或在Chrome调试器的“源”选项卡中打开代码,然后单击左边距以设置断点
无论采用哪种方式,在post_comment()
函数的开头、在submit()
回调的开头(即$.ajax()
行)以及在每个成功
和完成
回调的开头设置断点
然后你可以看到代码的进展,看看变量等等。这会给你更多的线索
这里有一个和。不要只查看开发人员工具中的错误。使用Chrome调试器在代码中设置断点,并查看它可以到达哪些断点
您可以添加调试器将代码>语句添加到要停止的代码中,或在Chrome调试器的“源”选项卡中打开代码,然后单击左边距以设置断点
无论采用哪种方式,在post_comment()
函数的开头、在submit()
回调的开头(即$.ajax()
行)以及在每个成功
和完成
回调的开头设置断点
然后你可以看到代码的进展,看看变量等等。这会给你更多的线索
这是一个and。试试这个
HTML:
姓名:
评论:
然后创建一个动作文件
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;
?>
如果代码不起作用,您需要编辑一些代码
HTML:
姓名:
评论:
然后创建一个动作文件
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.'">
如果你这样称呼它:
post_comment('.$post_id.')
那么您的变量将是:
x = "#c.$post_id.";
y = "#comments.$post_id.";
在这种情况下,您的ID是错误的,您有额外的单引号。根据:
<div class="posted_comments" id="comments'.$post_id.'"></div>
<form method="post" id="c'.$post_id.'">
如果你这样称呼它:
post_comment('.$post_id.')
那么您的变量将是:
x = "#c.$post_id.";
y = "#comments.$post_id.";
在这种情况下,您的ID是错误的,您有额外的单引号。您应该考虑为我们提供一个与您相关的代码。帮助你会容易得多。帮我拉小提琴。。如果可以提示:应该使用描述性变量名,而不是x
或y
尝试删除代码>最后,这不应该是here@TecHunter-除非OP使用的是XHTML(希望不是这种情况-没有好的理由,任何人都不应该使用XHTML),否则没有必要在任何标记上使用/>
结束符。在HTML中,如<代码> <代码>和<代码>
<代码>的标签在没有<代码> /> <代码>的情况下是有效的。您应该考虑为我们提供一个与您相关的代码。帮助你会容易得多。帮我拉小提琴。。如果可以提示:应该使用描述性变量名,而不是x
或y
尝试删除代码>最后,这不应该是here@TecHunter-除非OP使用的是XHTML(希望不是这种情况-没有好的理由,任何人都不应该使用XHTML),否则没有必要在任何标记上使用/>
结束符。在HTML中,像
和
这样的标记在没有/>
的情况下是有效的。submit函数已经返回了false
,它执行preventDefault()
和stopPropagation()
。还请注意,如果处理按钮上的单击
事件,则如果在表单的输入字段中使用Enter键,则无法截获提交。您应该改为处理表单上的submit
事件,原始代码就是这样做的原始代码段中的code>嵌套在传递给submit的函数中。实际的事件回调(post_comment
)不会返回false。OP应该在post_comment
的末尾移动return false
,提交函数已经返回false
,它执行preventDefault()
和stopPropagation()
。还请注意,如果处理按钮上的单击
事件,则如果在表单的输入字段中使用Enter键,则无法截获提交。您应该改为处理表单上的submit
事件,原始代码就是这样做的原始代码段中的code>嵌套在传递给submit的函数中。实际的事件回调(post_comment
)不会返回false。OP应在post\u comment