在按钮单击时调用js函数不起作用

时间:2013-11-05 23:23:10

标签: javascript jquery html

单击时没有运行的任何原因?我似乎无法弄明白。

<button id="button-container-like" onclick="rate($(this).attr(\'id\'))"><span class="thumbs-up"></span>Like</button>

<script>
function rate(rating){
var data = 'rating='+rating+'&id=<?php echo $eventId; ?>&userid56=<?PHP echo $userid55; ?>';

$.ajax({
   type: 'POST',
   url: 'includes/rate.php', //POSTS FORM TO THIS FILE
   data: data,
   success: function(e){
   $(".ratings").html(e); //REPLACES THE TEXT OF view.php
}
});
}
</script>

2 个答案:

答案 0 :(得分:1)

你去:

<button id="button-container-like" ><span class="thumbs-up"></span>Like</button>

<script>
    jQuery(document).ready(function () {

        $("#button-container-like").click(function() {
            rate($(this).attr("id"));
        });

        function rate(rating) {
            var data = 'rating=' + rating + '&id=<?php echo $eventId; ?>&userid56=<?PHP echo $userid55; ?>';

            $.ajax({
                type: 'POST',
                url: 'includes/rate.php', //POSTS FORM TO THIS FILE
                data: data,
                success: function(e) {
                    $(".ratings").html(e); //REPLACES THE TEXT OF view.php
                }
            });
        }
    });


</script>

答案 1 :(得分:0)

如果此代码中没有其他内容,请从onclick代码中删除斜杠。它们是不必要的并导致错误。

onclick="rate($(this).attr('id'))"