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条)

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动