【发布时间】:2014-08-29 00:45:02
【问题描述】:
首先,我不是很熟悉 JQuery 和 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">×</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);
【问题讨论】:
-
$.when 执行你的方法...这就是为什么它显示在页面加载
标签: javascript jquery ajax twitter-bootstrap