我正在为我的网站创建“喜欢和不喜欢”按钮,在我的本地服务器上我使用了单独的文件,它工作正常,但是当我在我的网站上安装时,它无法正常工作。

我使用此代码从网址栏中获取一些信息

    <?php
    error_reporting (0);
    include "includes/session.php";

    include 'database/db.php';

    extract($_REQUEST);
    if(isset($_GET["i"]))
    {
    $pid=($_GET['p']);
    $lid=($_GET['i']);
    }
    ?>

    <?php
    $likquery = mysql_query("select * from likes where userid=$id and postid=$lid");
    if(mysql_num_rows($likquery)==1){

    ?>
        <a class="unlike" id="<?php echo $lid ?>">UnLike</a>&nbsp;<span class="like_update" id="<?php echo $lid ?>"></span>

    <?php }else{?>

        <a class="like" id="<?php echo $lid ?>">Like</a>



        <?php
    }?>


之后,我使用脚本

    <script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function(){

    $('body').on('click','.like',function(){


    var postId = $(this).attr('id');

    var postData = 'postid='+postId+'&uid=<?php echo $id ?>';

    $.ajax({

        type: "POST",
        url: "like_notes",
        data: postData,
        cache: false,
        success: function(){

            $('#'+postId).text('UnLike').addClass('unlike').removeClass('like');
        }

    });

    })



    $('body').on('click','.unlike',function(){

    var postId = $(this).attr('id');

    var postData = 'postid='+postId+'&uid=<?php echo $id ?>';

    $.ajax({

        type: "POST",
        url: "likeun_notes",
        data: postData,
        cache: false,
        success: function(){

            $('#'+postId).text('Like').addClass('like').removeClass('unlike');
        }

    });

    })

    });

    </script>


而且我没有得到任何回应,但是当我使用

<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){

$(".like").click(function(){

    var postId = $(this).attr('id');

    var postData = 'postid='+postId+'&uid=<?php echo $id ?>';

    $.ajax({

        type: "POST",
        url: "like_notes",
        data: postData,
        cache: false,
        success: function(){

            $('#'+postId).text('UnLike').addClass('unlike').removeClass('like');
        }

    });

})


$(".unlike").click(function(){

    var postId = $(this).attr('id');

    var postData = 'postid='+postId+'&uid=<?php echo $id ?>';

    $.ajax({

        type: "POST",
        url: "likeun_notes",
        data: postData,
        cache: false,
        success: function(){

            $('#'+postId).text('Like').addClass('like').removeClass('unlike');
        }

    });

})

});

</script>

only once it is changing the value

最佳答案

好的,第二个版本只能使用一次,因为在开始时,该事件位于按钮上,然后您通过更改按钮的类和文本来重新呈现该按钮,因此该事件将解除绑定。

与第一个版本相同。 jQuery表示:事件处理程序仅绑定到当前选择的元素。当您的代码调用.on()...时,它们必须存在于页面上。如果将新HTML注入页面,请选择元素并在将新HTML放入页面后附加事件处理程序。

因此,更改按钮后请重新绑定事件。

我建议是这样的:

function bindEvents() {
    $('.like').off().on('click', function(){
        var postId = $(this).attr('id');

        var postData = 'postid='+postId+'&uid=' + <?php echo $id ?>;

        $.ajax({

            type: "POST",
            url: "like_notes",
            data: postData,
            cache: false,
            success: function(){

            $('#'+postId).text('UnLike').addClass('unlike').removeClass('like');
           //rebind events here
           bindEvents();
        }
    });
     $('.unlike').off().on('click', function(){
        var postId = $(this).attr('id');

        var postData = 'postid='+postId+'&uid=' + <?php echo $id ?>;

        $.ajax({

            type: "POST",
            url: "likeun_notes",
            data: postData,
            cache: false,
            success: function(){

            $('#'+postId).text('Like').addClass('like').removeClass('unlike');
           //rebind events here
           bindEvents();
        }
    });
}


$(document).ready(function(){
    bindEvents();
});


另请注意,在var postData = 'postid='+postId+'&uid=<?php echo $id ?>';代码中,php部分不会被视为php。它只是一个字符串。因此,假设代码位于php文件中,请用以下内容替换该部分:

var postData = 'postid='+postId+'&uid=' + <?php echo $id ?>;

关于javascript - 使用ajax调用在php和mysql中面对Make Like Different Button,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25947731/

10-12 00:38
查看更多