drvvvuyia15070493 2017-02-13 07:41
浏览 39

在js发布的外部文件中通过Ajax访问id div

I want a full admin panel Ajax write And a page that details of the bank called with Ajax... I poured this information on a div that performs the button off and on

my cod is :

HTML

<html>
<head>

  <script src="file/js/Connection.js"></script>

</head>
<body>

  <div class="row" id="box"></div>

</body>
</html>

Connection File js cod :

$(document).ready(function() {
   show_all();
});


      function show_all() {
         work = "select";
          $.ajax({
              type: "POST",
              url: "server.php",
              data: "work="+work,
              success: function(data) {
                  $("#box").html(data);  
              } 
          });  
      }

and file server.php :

<?php

$pdo = new PDO('mysql:host=localhost;dbname=Contact', 'root', '');

if (isset($_POST['work'])) {
    $work = $_POST['work'];
    if ($work == 'select') {
        $qcomment = $pdo->query("SELECT * FROM myfeilds");
        while ($XXX = $qcomment->fetch()) {
            $Z1 = $XXX['id'];
            $Z2 = $XXX['name'];
            $Z3 = $XXX['active'];
            echo '


<div class="col-lg-3">
  <div class="row" id="back">
    <div class="col-lg-8" id="Fname">
      <span class="glyphicon glyphicon-check"></span>
        <label>' . $Z2 . '</label>
    </div>
    <div class="col-lg-4" id="Fbtn"> ';

      if ($Z3 == 1) { echo '

      <div class="btn btn-on" id="' . $Z1 . '">
        <div> <span class="glyphicon glyphicon-remove"></span></div>
        <div><span class="glyphicon glyphicon-ok"></span></div>
      </div>';   

       } else { echo '

      <div class="btn btn-off" id="' . $Z1 . '">
        <div> <span class="glyphicon glyphicon-remove"></span></div>
        <div><span class="glyphicon glyphicon-ok"></span></div>
      </div>';
       } echo '


    </div>
  </div>
</div>
';
        }
    }
}

?>

And in the end I tried to write that off and turn on the javascript code

$(".btn").on('click',function(e){
    if($(this).hasClass("btn-on")){
    $(this).removeClass("btn-on");
    $(this).addClass("btn-off");

        }
        else {
    $(this).removeClass("btn-off");
    $(this).addClass("btn-on"); 

        }
});

And they told me because the select performed in an external file Then you must use this code to work correctly

$(document).on("click",".btn",function(event) {
    if($(this).hasClass("btn-on")){
    $(this).removeClass("btn-on");
    $(this).addClass("btn-off");

        }
        else {
    $(this).removeClass("btn-off");
    $(this).addClass("btn-on"); 

        }
});

This code works, but only the first time that I entered the Fever this page And if I get another tab and go back again, does not work...

what do I do :)

  • 写回答

1条回答 默认 最新

  • doudou890510 2017-02-13 07:46
    关注

    You can try using Comet in Jquery http://www.screenr.com/SNH

    Hope this helps.

    Regards,

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?