douya5194 2017-12-13 19:08
浏览 48
已采纳

Ajax加载页面+ JQuery点击不起作用[重复]

This question already has an answer here:

Why does my ajax not work?

These are my pages.

I'm using JQuery to check click event and using ajax to load the responder page.

What is wrong in this code?

index.php

<html>
  <head>
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
      $(document).ready(function() {
          $('#clicket').click(function() {
            $.ajax({
              url: 'res.php?rand=' + Math.random(),
              type: 'GET'
              success: function(results) {
                alert(results);
              }
            });
          });
        });
      </script>
    </head>
  <body>
    <button id="clicket">Hi</button>
  </body>
</html>

Responder page:

res.php:

<?php
  echo "Worked!";
?>

UPDATE: I changed my javascript code above.

</div>
  • 写回答

2条回答 默认 最新

  • duanci6484 2017-12-13 19:11
    关注

    Put your logic in a document ready. Since your script is in your head, the markup in the body hasn't been loaded into the DOM yet. In order to make your script wait until the DOM has been built, put the logic in a document ready to delay it until that time.

    $(document).ready(function() {
      $('#clicket').click(function() {
        $.ajax({
          url: 'res.php?rand=' + Math.random(),
          type: 'GET',
          success: function(results) {
            alert(results);
          }
        });
      });
    });

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line