jQuery函数不适用于AJAX返回/渲染页面

时间:2018-10-09 19:20:19

标签: javascript jquery ajax

在使用从ajax请求返回的代码运行jquery任务时,我遇到问题。

包含AJAX请求的主页(索引)。来自AJAX调用的responseText显示在select游戏类内部。 selection_game.php中的所有内容均不响应任何jquery代码。

$(document).ready(function() {
  var cast = "";

  $("#game_backer").click(function() {
    alert("yes");
  });
  $(".sub-game-top").click(function() {
    $(".sub-game-top").fadeOut(100);
  });
  $(".select-game button, #game_backer button").click(function() {
    cast = $(this).parent().attr("id");

    $(".select-game > div").fadeOut(400, function() {

      if (window.XMLHttpRequest) {
        ajax = new XMLHttpRequest();
      } else {
        ajax = new ActiveXObject("Microsoft.XMLHTTP");
      }
      ajax.onreadystatechange = function() {
        if (this.readyState === 4 & this.status === 200) {
          if (this.responseText != "") {
            document.getElementsByClassName("select-game")[0].innerHTML = this.responseText;
            $(".sub-game-top").fadeIn(400);
            //document.getElementsByClassName("opacitynull")[0].style.opacity = "1";
          } else {
            alert(cast + " - none");
          }
          //alert(this.responseText);
        }
      }

      ajax.open('POST', 'includes/selecting_game.php', true);
      ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      ajax.send('gameValue=' + cast);
    });
  });
});
<div class="select-game">
  <div class="select-game-three">

    <div class="gameshoverAction">
      <div class="GHAimg"><img src="images/overall/csgo-bg.png" alt="Counter strike background"></div>
      <div>
        <div><img src="images/overall/csgo.png" alt="Counter strike"></div>
        <div class="gameshoverTrigger">
          <div id="csgo_competitive"> <button>Competitive</button></div>
          <div id="csgo_casual"> <button>Casual</button> </div>
          <div id="csgo_coaching"> <button>Coaching</button> </div>
        </div>
      </div>
    </div>
  </div>
</div>

这是selection_game.php页面。单击div中的任何按钮时,此代码将显示在选择游戏类中。这是运行ajax函数时不响应任何jquery事件或函数的代码。

    <?php 

if (!empty($_POST["gameValue"])) {

    $GV = htmlentities($_POST["gameValue"]);


    if ($GV == "csgo_competitive") { 

        //one
    ?>

        <div class="sub-game-top sub-select-game-one hidden">

            <div id="game_backer">
                <button>« Back</button>
            </div>

            <div class="gameshoverAction" id="team_builder">
                <div class="GHAimg"><img src="images/overall/csgo-big-bg.png" alt="CSGO background"></div>
                <div class="gameshoverTrigger">
                    <div><img src="images/overall/csgo.png" alt="CSGO image"></div>
                    <div>
                        <div id="csgo_competitive"><h1>Team builder</h1></div>
                    </div>
                </div>
            </div>

        </div>

    <?php 
    } else if ($GV == "csgo_casual") { 

        // link to casual gaming page
    ?>

        <p>Dette er en test</p>

    <?php 
    } 

}
?>

0 个答案:

没有答案