在Bootstrap Navbar中调用时,Ajax请求不起作用

时间:2016-10-13 20:45:21

标签: javascript ajax twitter-bootstrap twitter-bootstrap-3

所以我正在学习JS / Ajax,我想尝试通过单击按钮将html内容加载到我的页面中。只要我在Navbar之外调用函数,一切都有效,我无法找出原因?

我正在尝试将“bestellung.html”中的html内容加载到ID为“main-well”的div中。一切正常,除非我想从我的Navbar中调用ladeBestellung()函数...内容被加载但被立即覆盖并消失......为什么?

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>QR2BUY Bestellübersicht</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Eigene Styles -->
    <link href="css/styles.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for 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/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
      <![endif]-->

  </head>
  <body>
    <div class="container"><!-- Haupt-Container -->
        <!-- Haupt-Navigationsleiste -->
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="navbar-header"><!-- Anfang Navigationsleiste-Header -->
                    <!-- Button für die mobile Ansicht -->
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                        <span class="sr-only">Navigation aufklappen</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#">
                        <span class="glyphicon glyphicon-qrcode"></span> QR2BUY
                    </a>
                </div><!-- Ende Navigationsleiste-Header -->
                <div id="navbar" class="navbar-collapse collapse">
                    <ul class="nav navbar-nav navbar-right">                        
                        <li><a href="#">Hilfe</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Konto <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Einstellungen</a></li>
                                <li><a href="#">Bestellhistorie</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="index.html">Logout</a></li>
                            </ul>
                        </li>
                    </ul>
                    <form class="navbar-form navbar-right"><!-- Anfang Suchfeld -->
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="Bestell-Nr. eingeben...">
                            <div class="input-group-btn">
                                <button class="btn btn-default" onClick="ladeBestellung()"><i class="glyphicon glyphicon-search"></i></button>
                            </div>
                        </div>                          
                    </form><!-- Ende Suchfeld -->                   
                </div>
            </div>
        </nav><!-- Ende Haupt-Navigationsleiste -->

        <div class="row"><!-- Start Main-Row -->
            <div class="col-xs-12"><!-- Start Main Col -->
                <div class="well" id="main-well">

                </div><!-- Ende Well -->
            </div><!-- Ende Main-Col -->

        </div><!-- Ende Main-Row -->




    </div><!-- Ende Haupt-Container -->


    <footer class="footer"><!-- Anfang Footer -->
        <div class="container">
            <div class="row">
                <div class="col-xs-12">
                    <p class="text-center">
                        <span>&copy; 2016 QR2BUY</span> - <a href="#">Impressum</a> - <a href="#">Kontakt</a>
                        <p>
                        </div>
                    </div>
                </div><!-- Ende Haupt-Container -->

            </footer><!-- Ende Footer -->


            <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <!-- Include all compiled plugins (below), or include individual files as needed -->
            <script src="js/bootstrap.min.js"></script>
            <script>
                var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function() {
                    if (xhr.readyState === 4) {
                        document.getElementById('main-well').innerHTML = xhr.responseText;
                    }
                };
                xhr.open('GET', 'bestellung.html');
                function ladeBestellung() {
                    xhr.send();
                }
            </script>
        </body>
        </html>

1 个答案:

答案 0 :(得分:0)

默认类型<button>是“submit”,它会重新加载页面并覆盖javascript生成的html - 所以解决方案是使用<button type="button"></button>