简体   繁体   English

为什么当我单击“喜欢”图像时,ajax函数无法执行?

[英]Why is the ajax function not executing when I click on the 'like' image?

I have a picture of a like button, when clicked, should update the score by 1. I am using ajax to execute this. 我有一个喜欢按钮的图片,单击该按钮后应将分数更新1。我正在使用Ajax执行此操作。 However, when I click the image nothing happens. 但是,当我单击图像时,没有任何反应。 When I hover my mouse on the image, the pointer does not even change to a hand. 当我将鼠标悬停在图像上时,指针甚至不会变为手。

index.php 的index.php

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/style.css"></link>
        <script src="js/jquery-1.11.3.min.js"></script>
        <script src="js/script.js"></script>
    </head>
<body>

    Welcome: <?php
        // Start the session
        session_start();
        //Use session variable created in login.php
        echo $_SESSION['username'];
    ?>

    <div class="friend">
    <?php
        while($row = $res->fetch_assoc())
        {
            echo "<image class='img' src='images/" .$row['image'] . "'></image>";
            echo "<div class='info'>";
            echo $row['name'] . "<br>";
            echo $row['surname'] . "<br>";
            echo "SCORE: " . ($row['likes'] - $row['dislikes']);
            echo "</div>";


        }       
    ?>

    <div class='friend_actions'>
            <image class='button' id='likes' src='images/like.jpg'></image><br>
            <image class='button' id='dislikes' src='images/dislike.jpg'></image>
    </div>

</body>

script.js 的script.js

$(document).ready(function () { 

    $('#likes').click(function() {
        $.ajax({
            url: 'likes.php',
            type:'post',
            data: {id: $('#id').val()},
            success: function (result) 
            {
                //Reload the current document
                location.reload();
            }

        });

});

likes.php likes.php

<?php
    require "php/conn.php";

    $db->query("UPDATE friends_list SET likes = likes + 1 WHERE id = 1");
?>
Here syntax error in script writing check 
this your code    
$(document).ready(function () {    
$('#likes').click(function() {   
    $.ajax({   
        url: 'likes.php',  
        type:'post',  
        data: {id: $('#id').val()},  
        success: function (result)   
        {  
            //Reload the current document  
            location.reload();  
        }  
});  //this end of ajax
}); //end of like event function
 where is document end ? 
 you can not complete document '});' so complete document function 

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

相关问题 为什么我的Javascript代码中的函数立即执行,但单击按钮后却不执行? - Why is the function in my Javascript code executing right away but not when I click the button? 当我单击编辑图像时,我希望我的编辑功能能够正常工作 - I would like to have my edit function to work when i click on the edit image 单击锚标记时,jQuery就绪函数执行两次 - jquery ready function executing twice when I click on an anchor tag jQuery mobile-为什么当我单击按钮时会有ajax调用? - Jquery mobile - Why there is ajax calling when I click button? 在后台执行Ajax函数时,如何在页面上添加自定义动画图像? - How to Add custom animated image on page when ajax function is executing in the background? 如何在执行时临时禁用click功能? - How to temporary disable the click function when it is executing? 在ajax调用时执行另一个功能的原因 - reason for executing another function when ajax calling 为什么当我在我的 aspx 页面中的 html 图像点击事件中调用 JavaScript 函数时,我的网页正在刷新? - why my webpage is refreshing when i am calling a JavaScript function in my aspx page on a html image click event? 为什么Ajax调用没有执行? - Why is the Ajax call not executing? 为什么当我点击复选框时没有调用函数onclick? - Why when I click on the checkbox doesn't call the function onclick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM