2017-02-13 24 views
1

发布的外部文件中通过Ajax访问ID div我想要一个完整的管理面板Ajax写和一个使用Ajax调用银行的详细信息的页面...我将此信息浇注在执行按钮关闭的div上和在由js

我的鳕鱼是:

HTML

<html> 
<head> 

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

</head> 
<body> 

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

</body> 
</html> 

连接文件JS鳕鱼:

$(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); 
       } 
      }); 
     } 

和文件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> 
'; 
     } 
    } 
} 

?> 

而在最后,我试着写关闭和打开的javascript代码

$(".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"); 

     } 
}); 

他们告诉我,因为在外部文件进行。然后选择您必须使用此代码才能正常工作

$(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"); 

     } 
}); 

此代码的工作,而只是说我进入了发烧此页面 第一次如果我得到另一个选项卡,再回去,不工作...

我该怎么做:)

回答