gpt4 book ai didi

php - 第二次没有AJAX效果

转载 作者:行者123 更新时间:2023-12-01 08:00:17 24 4
gpt4 key购买 nike

我目前正在使用 PHP、SQL、jQuery 和 AJAX 构建一个评级系统。

我工作得很好,除了一个小细节。当我第一次评分时,结果成功发送到rate.php 文件,保存在我的数据库中,并将新结果发送回我的评分页面。但当我尝试再次评分时,什么也没有发生。

甚至 jQuery“悬停效果”也不起作用。我必须重新加载整个页面,然后才能再次评分。

我尝试更改 onbindclick 。去那里帮忙。我还尝试添加 cache: false,并更改successdone ,最后还有document.getByIdcontent.html(result);

我对 ajax 还很陌生,所以也许有更简单的方法来做到这一点。

我听说过一些叫做 JSON 的东西,但我不知道这是否适用于此。

<小时/>

这是我在 site.php 上的 jQuery 和 AJAX:

$(document).ready(function() {


$('span').hover(function() {
$(this).prevAll().andSelf().toggleClass('my');

});



$('span').on('click', function() {

// Define data to send
var id = $(this).parent().attr("id");
var star = $(this).attr("class");

// Put data together and send request with ajax
var post = "id="+id+"&stars="+star;
$.ajax( {
url:"includes/rate.php",
cache: false,
data: post,
success:function(result) {
document.getElementById(id).innerHTML=result;
}

});

});

});

在 site.php 上加载当前评级的 PHP:

http://pastie.org/private/qql0ajaq9lfjqmisgvmzkq

和rate.php:

http://pastie.org/private/kgufwlmx69t8vxjugx9zyg

希望有人能帮我解决这个问题。

最佳答案

您正在替换元素,这导致您的点击事件处理程序停止。尝试将点击处理程序附加到文档。

更改:

$('span').on('click', function() {

致:

$(document).on('click', 'span', function() {

由于您使用的是 jQuery,因此您还可以在代码中更改此内容:

document.getElementById(id).innerHTML=result;

致:

$('#' + id).html(result);

关于php - 第二次没有AJAX效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20364074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com