单击按钮时发出AJAX请求

Make AJAX request when button is clicked

本文关键字:AJAX 请求 按钮 单击      更新时间:2023-09-26

首先,我对JQueryAJAX不是很熟悉。我通过尝试我在网上找到的所有东西,想出了以下代码,老实说,我并不完全理解。

我想展示一个引导模式,当点击按钮时,它包含数据库中的记录。我的问题是,当页面加载时,它已经打开了模态,而不是单击按钮时。

<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);

您正在立即调用函数,而不是传递引用。

$("#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');
    });
}

快看看这把小提琴

$.当执行您的方法时。。。这就是为什么它显示在页面加载上。

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

你必须这样做。

或者,如果您真的想使用$.when(可能是因为您有多个ajax调用),如下所示:

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