2014-08-29 81 views
-1

首先的時候,我不是真的熟悉JQuery的AJAX。通過嘗試我在網上找到的所有內容,我想出了下面的代碼,並且說實話我並不完全理解它。讓AJAX請求按鈕被點擊

我希望顯示一個引導模式,其中包含單擊按鈕時數據庫中的記錄。 我的問題是,當頁面加載時,它已經打開了模式,而不是點擊按鈕時。

<button id="modalData">Load Modal</button> 

<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    <div class="modal-dialog modal-lg"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
       <h4 class="modal-title" id="myModalLabel">Plantilla</h4> 
      </div> 
      <div class="modal-body"> 
       <table class="table table-striped table-bordered table-hover"> 
        <thead> 
         <tr> 
          <th>Item Code</th> 
          <th>New Item Code</th> 
          <th>Position</th> 
          <th>Station</th>          
         </tr> 
        </thead> 
        <tbody> 
         <c:forEach var="plantilla" items="${sessionScope.plantilla}">          
          <tr> 
           <td><c:out value="${plantilla.getItemCode()}"/></td> 
           <td><c:out value="${plantilla.getNewItemCode()}"/></td> 
           <td><c:out value="${plantilla.getPosition()}"/></td> 
           <td><c:out value="${plantilla.getStation()}"/></td>                   
          </tr> 
         </c:forEach> 
        </tbody> 
       </table> 
      </div> 

     </div> 
    </div> 
</div> 

這是我的腳本:

$("#modalData").click(loadPlantilla()); 

$.when(loadPlantilla()).done(function() { 
    $('#modal').modal('show'); 
}); 

function loadPlantilla() {     
    return $.ajax({ 
     url: './TryAjax', 
     type: 'POST', 
     error: function() { 
      alert('Ajax readyState: ' + xhr.readyState + '\nstatus: ' + xhr.status + ' ' + err); 
     } 
    }); 
} 

,這是我的Servlet:

PlantillaViewDao dao = new PlantillaViewDao(); 
List<PlantillaView> plantilla = dao.read(); 

HttpSession session = request.getSession(); 
session.setAttribute("plantilla", plantilla); 
+0

$ .when執行你的方法...這就是爲什麼它顯示在頁面加載 – jvecsei 2014-08-29 00:51:10

回答

2

你調用該函數,而不是立即的傳遞引用。

$("#modalData").click(loadPlantilla); 

你的Ajax請求還可以使用.done

function loadPlantilla() {     
    $.ajax({ 
     url: './TryAjax', 
     type: 'POST', 
     error: function() { 
      alert('Ajax readyState: ' + xhr.readyState + '\nstatus: ' + xhr.status + ' ' + err); 
     } 
    }).done(function() { 
     $('#modal').modal('show'); 
    }); 
} 

快速瀏覽一下這個fiddle

+0

爲什麼'.click(loadPlantilla);'沒有'()'?看起來,當它有'()'時,它會在頁面加載時調用該方法。 – lxcky 2014-08-29 01:09:09

+1

因爲你傳遞了一個引用 - 這是一個回調方法。當鏈接被點擊時它會被調用。 '()'執行該方法,以便傳遞此方法的返回值。 – jvecsei 2014-08-29 01:13:17

1

$。當執行你的方法......這就是爲什麼它是在頁面加載顯示。

function loadPlantilla() {     
    $.ajax({ 
     url: './TryAjax', 
     type: 'POST' 
    }).done(function() { 
     $('#modal').modal('show'); 
     }); 
} 

你必須這樣做。

或者,如果你真的想用$。當(也許是因爲你有一個以上的AJAX調用)是這樣的:

$("#modalData").click(function() { 
    $.when(loadPlantilla(), method2(), method3()).done(function() { 
     $('#modal').modal('show'); 
    }); 
}); 
+0

我只有一個AJAX調用。我不知道「何時」是用於進行多個ajax調用。就像我所說的,我剛開始學習JQuery和AJAX。我用你的建議替換了我的'loadPlantilla()'方法,它不再顯示載入的模式。但問題是當我點擊按鈕時。它似乎沒有工作。 – lxcky 2014-08-29 00:58:54

+0

好的。所以第一個解決方案應該適合你的問題。 – jvecsei 2014-08-29 01:00:17

+0

我編輯了我的代碼。其實你應該再次考慮你的「錯誤」代碼。 https://api.jquery.com/jQuery.ajax/。正如你看到3個參數被傳遞給函數'Type:Function(jqXHR jqXHR,String textStatus,String errorThrown)'。 – jvecsei 2014-08-29 01:02:29