【问题标题】:I can't get my javascript function to work?我无法让我的 javascript 函数工作?
【发布时间】:2011-12-01 00:02:17
【问题描述】:

我无法让函数运行onClick

我的问题是我无法让like_add 工作,或者是onclick 不工作,我不知道这就是我需要帮助的原因。

如果我提醒数据,它可以工作,但我需要它来制作 = .text(data)

测试.php

<?php
//int
include '../scripts/connect_to_mysql.php';
session_start();
$_SESSION['user_id'] = '3';

//articles
function get_post() {
    $posts = array();

    $query = mysql_query("SELECT * FROM `post`");
    while (($row = mysql_fetch_assoc($query)) !== false) {
        $posts[] = array(
            'post_id' => $row['id'],
            'post_body' => $row['post_body'],
            'likes' => $row['likes']
        );
    }
    return $posts;
}

?>

<!doctype html>
<head>
<title>TEST</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
function like_add(post_id) {
$.ajax({
                type: "POST",
                url: "ajax/like_add.php",
                data: "post_id=" + post_id,
                success: 
                    function vote_get(post_id) {

                        $.ajax({
                                    type: "POST",
                                    url: "ajax/like_get.php",
                                    data: "post_id=" + post_id,
                                    success: function(data) { alert(data);  },
                                    error: function(msg){
                                        alert(msg);
                                      }             
                        });
                    },
                error: function(msg){
                    alert(msg);
                }
            });
}



</script>
</head>
<body>
<?php
    $posts = get_post();
    if (count($posts) == 0) {
        echo 'Sorry, there are no posts.';
    } else {
        echo '<ul>';
        foreach($posts as $post) {
            echo '<li><p>', $post['post_body'] ,'</p><p><a href="#" onclick="like_add('.$post['post_id'].')">Like</a>&nbsp;<span id="post_'. $post['post_id'].'_likes">', $post['likes'] ,'</span> people like this</p></li>';
        }
        echo '</ul>';

    }
?>
</body>
</html>

【问题讨论】:

  • 控制台对您说了什么? !因为我认为你的代码有一些错误
  • 首先检查您的所有输出是否符合预期,并减少代码直到您可以工作。只要某些东西按预期工作,添加一些代码,看看它是否仍然有效。
  • 页面加载后,您应该避免使用document.write - 如果您只是在调试,请使用alert()console.log()like_add() 在您的大代码块中的版本调用“like_get.php”,但您还显示了 like_add.php 的代码,因此它可能调用了错误的东西。它还测试“成功”的返回值,但您的 like_add.php 正在返回“作品”。
  • 当我点击链接没有任何反应,我已经尝试减少代码,我会再试一次

标签: php javascript short


【解决方案1】:

试试这个:

<script type="text/javascript">
function like_add(post_id) {
$.ajax({
                type: "POST",
                url: "ajax/like_add.php",
                data: "post_id=" + post_id,
                success: function(post_id){
               vote_get(post_id);
               },

                error: function(msg){
                    alert(msg);
                }
            });
}
function vote_get(post_id) {

                        $.ajax({
                                    type: "POST",
                                    url: "ajax/like_get.php",
                                    data: "post_id=" + post_id,
                                    success: function(data) { $('#post_'+post_id+'_likes').text(data);  },
                                    error: function(msg){
                                        alert(msg);
                                      }             
                        });
                    }


</script>

【讨论】:

  • 对不起,我真的以为那行不通,就像我的代码找不到 like_add 函数
  • 在 $.ajax 调用之前添加一个警报,像这样 alert('function found!'); ,这告诉你它是否可以找到函数
  • 尝试从 onclick 中删除 ;return false;
  • 我发现如果我不添加函数like_get它可以工作,有没有办法让它与包含的函数一起工作??
  • 在您的代码中发现了一个小语法问题:onclick="like_add(', $post['post_id'] ,');" 应该是:onclick="like_add('.$post['post_id'].')"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多