ajax加载后,bootstrap toggle不起作用

时间:2015-09-15 12:41:23

标签: javascript jquery html ajax twitter-bootstrap

我正在使用ajax加载来获取我页面上的一些内容。我正在使用bootstrap 3和bootstrap切换。加载内容时,bootstrap 3内容工作正常(您可以清楚地看到面板 - 主要)。但是引导切换内容没有加载(你可以看到一些复选框)。是否有人知道如何使这个工作?或者我应该注意其他一些拨动开关?如果是哪一个?

编辑:我还没有测试过事件绑定,因为我无法通过引导切换css来处理加载了ajax的代码。事件绑定与css无关吗?

  1. 主html代码(带有ajax加载的代码)使用的脚本
  2. 主要HTML代码
  3. 代码被主html代码加载到div中
  4. if (strcmp($_SESSION['pass'], $password))  {
        echo 'you are logged in';
    } else {
        echo'<form method="POST" action="">
        <input type="password" name="pass">
        </form>';
    }
    
    <!-- jQuery -->
    <script src="../bower_components/jquery/dist/jquery.min.js"></script>
    
    <!-- Bootstrap Core JavaScript -->
    <script src="../bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
    
     <!-- Bootstrap Toggle JavaScript -->
    <script src="../bower_components/bootstrap-toggle-master/js/bootstrap-toggle.min.js"></script>
    
    <!-- Metis Menu Plugin JavaScript -->
    <script src="../bower_components/metisMenu/dist/metisMenu.js"></script>
    
    <!-- Custom Theme JavaScript -->
    <script src="../dist/js/sb-admin-2.js"></script>
    
    <!-- Custom Domotica JavaScript -->
    <script> $(document).ready(function(){
            // Set trigger and container variables
            var trigger = $('#side-menu li ul li a'),
            container = $('#page-wrapper');
            
            // Fire on click
            trigger.on('click', function(){
                
              // Set $this for re-use. Set target from data attribute
              var $this = $(this),
                target = $this.data('target');       
                alert("target: " + target)
              // Load target page into container
              container.load(target + '.html', function (response, status, xhr) {});
              // Stop normal link behavior
              return false;
            });
          });
    </script>

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="description" content="Home automation web page">
      <meta name="author" content="">
    
      <title>Home</title>
    
      <!-- Bootstrap Core CSS -->
      <link href="../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    
      <!-- Bootstrap toggle -->
      <link href="../bower_components/bootstrap-toggle-master/css/bootstrap-toggle.min.css" rel="stylesheet">
    
      <!-- MetisMenu CSS -->
      <link href="../bower_components/metisMenu/dist/metisMenu.min.css" rel="stylesheet">
    
      <!-- Custom CSS -->
      <link href="../dist/css/sb-admin-2.css" rel="stylesheet">
    
      <!-- Custom Fonts -->
      <link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    
      <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
      <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
      <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
            <![endif]-->
    
    </head>
    
    <body>
    
      <div id="wrapper">
    
        <!-- Navigation -->
        <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html">Home</a>
          </div>
          <!-- /.navbar-header -->
    
          <ul class="nav navbar-top-links navbar-right">
    
    
            <li class="dropdown">
              <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                <i class="fa fa-bell fa-fw"></i>  <i class="fa fa-caret-down"></i>
              </a>
              <ul class="dropdown-menu dropdown-alerts">
                <li>
                  <a href="#">
                    <div>
                      <i class="fa fa-comment fa-fw"></i> Failure
                      <span class="pull-right text-muted small">No respons from Controller 1</span>
                    </div>
                  </a>
                </li>
    
              </ul>
              <!-- /.dropdown-alerts -->
            </li>
            <!-- /.dropdown -->
            <li class="dropdown">
              <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                <i class="fa fa-gear fa-fw"></i>
              </a>
              <!-- /.dropdown-user -->
            </li>
            <!-- /.dropdown -->
            <li class="dropdown">
              <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                <i class="fa fa-sign-out fa-fw"></i> 
              </a>
            </li>
            <!-- /.dropdown -->
          </ul>
          <!-- /.navbar-top-links -->
    
          <div class="navbar-default sidebar" role="navigation">
            <div class="sidebar-nav navbar-collapse">
              <ul class="nav" id="side-menu">
                <li>
                  <a href="index"><i class="glyphicon glyphicon-home"></i> House<span class="fa arrow"></span></a>
                  <ul class="nav nav-second-level">
                    <li>
                      <a href="#">Weather</a>
                    </li>
                    <li>
                      <a href="#">Energy</a>
                    </li>
                    <li>
                      <a href="#">Water</a>
                    </li>
                  </ul>
                </li>
                <li>
                  <a href="#"><i class="glyphicon glyphicon-star"></i> 1st floor<span class="fa arrow"></span></a>
                  <ul class="nav nav-second-level" id="side-menu2">
                    <li>
                      <a href="#" data-target="index">Hallway</a>
                    </li>
                    <li>
                      <a href="#">Main bedroom</a>
                    </li>
                  </ul>
                  <!-- /.nav-second-level -->
                </li>
                <li>
                  <a href="#"><i class="glyphicon glyphicon-star"></i> main floor<span class="fa arrow"></span></a>
                  <ul class="nav nav-second-level">
                    <li>
                      <a href="#">Kitchen</a>
                    </li>
                    <li>
                      <a href="#">Living room</a>
                    </li>
                    <li>
                      <a href="#">Garage</a>
                    </li>
                  </ul>
                  <!-- /.nav-second-level -->
                </li>
                <li>
                  <a href="#"><i class="glyphicon glyphicon-star"></i> Basement</a> 
                  <!-- /.nav-second-level -->
                </li>
                <li>
                  <a href="#"><i class="glyphicon glyphicon-tree-deciduous"></i> Garden</a> 
                  <!-- /.nav-second-level -->
                </li>
              </ul>
            </div>
            <!-- /.sidebar-collapse -->
          </div>
          <!-- /.navbar-static-side -->
        </nav>
    
        <div id="page-wrapper">
    
        </div>
        <!-- /#page-wrapper -->
    
      </div>
      <!-- /#wrapper -->
    </body>
    
    </html>

2 个答案:

答案 0 :(得分:8)

确保正在正确加载bootstrap-toggle js文件和css文件。具体检查它们是否在您的链接指向的位置,或者甚至使用下面列出的cdn版本:

<=

您的路径必定是错误的,或者您忘记完全包含js文件,您应该在控制台中看到有关此问题的警告,否则代码工作正常。

关于动态加载的内容。

第一个元素有效,因为在首次加载html之后,<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.0/css/bootstrap-toggle.min.css" rel="stylesheet"> <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.0/js/bootstrap-toggle.min.js"></script> 会查找具有属性bootstrap-toggle.min.js的所有元素,并在应用插件时调用data-toggle="toggle"。这仅在页面加载时发生。

如果您稍后添加了更多切换按钮,则需要通过.bootstrapToggle()自行初始化它们。我已经更新了下面的例子来模拟动态添加切换并解释我将采取的方法。有关更多详细信息,请参阅代码中的注释。

&#13;
&#13;
.bootstrapToggle()
&#13;
      // timeout to simulate ajax
setTimeout(function(){ 
   // add an element dynamically,
  $('.table').append('<tr><td>Testswitch0</td><td><label><input type="checkbox" data-toggle="toggle" data-onstyle="success" id="S2" data-size="mini"></label></td></tr>');
    
    // now that we have dynamically loaded elements
    // we need to initialize any toggles that were added
    // you shouldn't re-initialize any toggles already present
    // but we also do want to have to figure out how to find the ones we added
    // instead, we'll destroy all toggles and recreate all new ones
    $("[data-toggle='toggle']").bootstrapToggle('destroy')                 
    $("[data-toggle='toggle']").bootstrapToggle();
  
}, 2000)
      
&#13;
&#13;
&#13;

答案 1 :(得分:3)

最有效的解决方案:

1-引导前加载Jquery。这里是Jquery,其次是Ajax,最后是bootstrap。

    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/js/bootstrap4-toggle.min.js"></script>

2-在ajax上调用bootstrapToggle完成:

    $(document).ajaxComplete(function() {
        $('input[type=checkbox][data-toggle^=toggle]').bootstrapToggle();
    });

3-注意:最后调用您的自定义脚本jquery