【问题标题】:jQuery on click function nothing happening点击功能上的jQuery没有发生任何事情
【发布时间】:2011-12-16 21:22:12
【问题描述】:

这个 jQuery 函数旨在将 num 的值发布到 PHP 页面,然后在正确的状态类中回显它,任何想法为什么什么都没有发生,我已经下载了 jQuery 并进一步调用了该文件(此处未显示)

感谢任何帮助!

<script>
 var num = 1;
                function ajax_post(){ 
$.ajax('javas.php', {
success: function(response) {
      $(".status").html(response);
}, 
data: "num=" + (++num)
});
}

function ajax_posta(){
$.ajax('javas.php', {
success: function(response) {
      $(".status").html(response);
}, 
data: "num=" + (--num)
});
}

$(document).ready(function() {
$('.eventer > .button').click(function () {
    ajax_post();
});
alert("lol");
});


</script>

这就是我所拥有的,这是我关于类的 php 代码。

<div id = 'eventcontainer' >

<?php

//Getting posts from DB

$event1 = mysql_query("SELECT post,date,memid FROM postaction WHERE memid = '$id' ORDER BY date DESC LIMIT 5;");

while ($row1 = mysql_fetch_array($event1))
{




$event = $row1['post'];
$timeposted = $row1['date'];

$eventmemdata = mysql_query("SELECT id,firstname FROM users WHERE id = '$id' LIMIT 1");

while($rowaa = mysql_fetch_array($eventmemdata))
{
    $name = $rowaa['firstname'];
    $eventlist = "$event <br> $name";
}

echo " <div class = 'eventer'> $timeposted <br>$eventlist <input name='myBtn'       type='submit' value='increment' onClick='javascript:ajax_post();'>
<input name='lol' type='submit' value='dec' onClick='javascript:ajax_posta();'></div>
<div class = 'status'></div>";
echo "<br>";

}


?>

【问题讨论】:

  • 请也添加您的标记。
  • 尝试将 $('.eventer > .button') 写入控制台,看看它是否返回任何 dom 对象。如果是这样,这次在控制台上尝试 $('.eventer > .button').first().click() 。你检查过php的错误吗?它有什么回报吗?正如我所看到的,您目前没有向 php 发布任何内容。加上函数中 $.post() 命令的使用(据我所知)是错误的,看起来你已经将它与 $.ajax() 调用混合在一起了。您应该查看api.jquery.com/jQuery.post 中的示例

标签: jquery html css ajax class


【解决方案1】:

$.post 改为 $.ajax

您可以重新安排参数以使其与 $.post 一起使用,但看起来您拥有的代码旨在与 $.ajax 一起运行。

还有,这里的里面

$(document).ready(function() {
    $('.eventer > .button').click(function () {
        var self = this;
        $.post('javas.php', function (data) {
            $(self).closest('.eventer').find('.status').html(data);
        })
    });
    alert("lol");
});

你确定你不是故意的:

$(document).ready(function() {
    $('.eventer > .button').click(function () {
        ajax_post();
    });
    alert("lol");
});

【讨论】:

  • 我必须将它保存在 ajax_post() 标签中以实现点击功能吗?
  • ajax_post() 看起来不错 - 您只需将 $.post 更改为 $.ajax。另请参阅我对另一个潜在问题的最后编辑
  • 我发布了所有相关部分,我似乎找不到任何错误
  • @foshoeiyyy - 创建一个重新创建问题的 jsfiddle
  • 也许 .button 必须是 .submit?
【解决方案2】:

如果您尝试在点击操作中添加警报,会发生什么?

也许你的动作并没有添加到你认为的对象上。

【讨论】:

    【解决方案3】:

    我认为您对 $.post 的调用中的参数顺序错误。应该是:

    $.post("url", { data:'something' }, function(result){
        //callback
    });
    

    【讨论】:

      【解决方案4】:

      看起来您正在混合使用 jQuery post 和 ajax 方法。尝试在您的帖子中添加数据参数(您的 num 变量)。

      $('.eventer > .button').click(function () {
          var self = this;
          $.post('javas.php', num,function (data) {
              $(self).closest('.eventer').find('.status').html(data);
          })
      });
      

      http://api.jquery.com/jQuery.post/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-05-13
        • 1970-01-01
        • 2011-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-07
        • 1970-01-01
        相关资源
        最近更新 更多