2016-10-13 5 views
0

JS/Ajaxを学習しているので、ボタンをクリックしてHTMLコンテンツをページに読み込もうとしました。私がNavbarの外で関数を呼び出す限り、すべてが機能し、なぜその理由が分かりませんか?Bootstrap Navbar内で呼び出されたときにAjaxリクエストが機能しない

"bestellung.html"から "main-well"というIDのdivにHTMLコンテンツを読み込もうとしています。私のNavbar内からladeBestellung()関数を呼び出すとき以外はすべて正常に動作します...コンテンツはロードされますが、すぐに上書きされて消えます...なぜ? <button>

<!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> 
+0

を使用することですあなたがそのボタンをクリックすると、ページの再読み込みますか? – jmargolisvt

+1

ボタンをクリックすると、フォームが送信されます。 'type =" button "属性を割り当てる必要があります。そうしないと、デフォルトで' submit'が行われます。この答えをチェックしてください。 - http://stackoverflow.com/questions/3314989/can-i-make-a-button- not-submit-a-form –

+0

これはまさに問題です。今はとても明らかですが、私はそれを考えなかったでしょう、ありがとう! – Tim

答えて

0

デフォルトのタイプは、ページをリロードするとJavaScriptが生成したHTMLを上書きした「提出」される - ので、解決策は<button type="button"></button>

関連する問題