行动后刷新某些数据

时间:2016-10-28 15:40:10

标签: javascript php

这是我的代码:

<input type="tekst" id="<?php echo $item->getId();?>">
<script>
    document.getElementById("<?php echo $item->getId();?>").value =<?php echo getVotesValue($item->getId());?>
</script>

我希望显示我的实际数量,在用户点击按钮之后,现在看到新的赞,我必须刷新页面,喜欢在服务器上保存。

这是我负责添加的代码:

<script>
$(function () {
    $('a.like').on('click', function () {
        var url = $(this).attr('href');
        $.post(url, {}, function () {
        });
        return false;
    });
});

所以再一次,我的目标是在点击我尝试后,在<input>标记中显示新的喜欢值:

$(function () {
    $('a.like').on('click', function () {
        var url = $(this).attr('href');
        $.post(url, {}, function () {
            document.getElementById("<?php echo $item->getId();?>").value =<?php echo getVotesValue($item->getId());?>
        });
        return false;
    });
});

但它没有按预期工作

1 个答案:

答案 0 :(得分:0)

你应该使用data-...

<a href="#" class="vote-up" data-id="<?php echo $item->getId(); ?>">Vote Up</a>

并单击功能

$('a.vote-up').click(function(e){
    var id = $(this).data('id');
    var url = 'likes.php?do=vote_up&id=' + id; // change to your PHP file
    $.post(url, {}, function(result){
        // you should return the total of likes in result
        $('#'+ id).val(result);
    });
});
相关问题