【问题标题】:How to load a content to modal with ajax while using Spring MVC frame work使用 Spring MVC 框架时如何使用 ajax 将内容加载到模态
【发布时间】:2017-09-15 17:30:15
【问题描述】:

ajax 和引导模式的新手,我正在尝试使用引导模式实现编辑功能。当用户单击编辑链接时,我需要将 DB 中的内容加载到模式中。我正在使用 ajax 执行此操作,下面是我如何实现它但是,我在浏览器上收到以下错误:

-script.js:62 Uncaught TypeError: Cannot read property 'on' of undefined at script.js:62(匿名)@script.js:62(我的 ajax 函数开始的行)

有人可以帮忙吗?谢谢。

代码片段:

我的 JSP:(加载模式的编辑链接)

    <c:forEach var="customer" items="${customers}" varStatus="status">
                                        <tr>
                                            <td><c:out value="${status.count}" /></td>
                                            <td><a title="Go to the Company Certificate Detail">${customer.customerName}</a></td>
                                            <td>${customer.contactName}</td>

                                            <td>${customer.street}</td>
                                            <td>${customer.state}</td>
                                            <td>${customer.zipCode}</td>
                                            <td>${customer.country}</td>
                                            <td>${customer.email}</td>



                                            <td>
                                                <div class="btn-group">
                                                    <button type="button"
                                                        class="btn btn-default dropdown-toggle"
                                                        data-toggle="dropdown">
                                                        Actions <span class="caret"></span>
                                                    </button>
                                                    <ul class="dropdown-menu" role="menu">
                                                        <li><a data-toggle="modal" data-target="#editCustomerModal" data-customer-id="${customer.id}">Edit Customer Detail</a></li>

                                                        <li><a data-toggle="modal" data-target="#deleteCustomerModal_${customer.id}" >Delete Customer</a></li>
                                                    </ul>
                                                </div>
                                            </td>

                                        </tr>

                                        <!--Delete Customer Modal  -->




</c:forEach>

我的编辑模式:(在forEach标签之外)

<!--Edit Customer Modal  --> 

<div id="createCustomerModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                Edit Customer
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">



                <table class="form-table">
                    <tbody>
                    <tr>
                    <td><input type="hidden" id="customerId" path="#" class="form-control" /></td></tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                class="not-required" for="pool-name">Customer Name:</label></td>
                            <td><input type="text" id="customerName" title="Company Name" path="#"
                                class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                class="not-required" for="expire-after">Contact Name:</label></td>
                            <td><input type="text" id="contactName" path="#"
                                class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                class="not-required" for="description">Street:</label></td>
                            <td><input type="text" id="street" path="#"
                                class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                class="not-required" for="description">State:</label></td>
                            <td><input type="text" id="state" path="#"
                                class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                class="not-required" for="expire-after">Zip-Code:</label></td>
                            <td><input type="text" id="zipCode" path="#"
                                class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                class="not-required" for="expire-after">Country:</label></td>
                            <td><input type="text" id="country" path="#"
                                class="form-control" /></td>
                        </tr>

                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                class="not-required" for="expire-after">Email:</label></td>
                            <td><input type="text" id="email" path="#"
                                class="form-control" /></td>
                        </tr>

                    </tbody>
                </table>


            </div>
            <div class="modal-footer">
                <div>
                    <input type="submit" id="createNewCustomer" value="Save"
                        class="btn btn-default" onClick="alert('To be Implemented');" />
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>

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

我的 script.js:

$(document).on('shown.bs.modal','#editCustomerModal', function () {

    var customerId = this.dataset.customerId;
    var id = $('#customerId').val(customerId);
    //var customerId= $('#customerId').val();
    var customerName= $('#customerName').val();
    var contactName= $('#contactName').val();
    var email= $('#email').val();
    var street= $('#street').val();
    var zipCode= $('#zipCode').val();
    var state= $('#state').val();
    var country= $('#country').val();

    $.ajax({
        type: "post",
        url: "/UtilityCertificateWebApplication/edit?id=" + customerId,
        cache: false, 
        contentType:'application/json',
        dataType: 'json',
        data:"customerName="+ customerName + "&contactName=" + contactName +  "&street=" + street +
          "&state=" + state + "&zipCode=" + zipCode + "&country=" + country +"&email=" + email ,
        success: function(response){
            alert("inside edit modal");
            var obj = JSON.parse(response);
            $('#customerName').val(obj.userName);
            $('#contactName').val(obj.userName);
            $('#email').val(obj.userName);
            $('#street').val(obj.userName);
            $('#zipCode').val(obj.userName);
            $('#state').val(obj.userName);
            $('#country').val(obj.userName);
        },
        error: function(){                      
            alert('Error while edit request..');
        }
    }); 


});

【问题讨论】:

    标签: javascript jquery ajax spring bootstrap-modal


    【解决方案1】:

    您的问题在这一行:

    $("#editCustomerModal_")+$('#customerId').val().on("show.bs.modal", function() {
    

    将该行更改为:

    $("[id^=editCustomerModal_").on("show.bs.modal", function() {
    

    现在,您的问题是获取源客户 ID?为此,您可以在模态显示事件中编写:

    var customerId = this.id.replace('editCustomerModal_', '');
    

    无论如何,我建议只使用一种模式。这意味着您需要更改此行:

    <li><a data-toggle="modal" data-target="#editCustomerModal_${customer.id}">Edit Customer Detail</a></li>
    

    到:

    <li><a data-toggle="modal" data-target="#editCustomerModal" data-customer-id="${customer.id}">Edit Customer Detail</a></li>
    

    现在,您的问题是:如何在模态显示时获取客户 ID:

    • 在锚点点击时添加监听器
    • 获取客户ID为:this.dataset.customerId

    $('[data-target="#editCustomerModal"]').on('click', function(e) {
        var customerId = this.dataset.customerId;
        $('#customerId').val(customerId);
    })
    $("#editCustomerModal").on("show.bs.modal", function (e) {
        var customerId = $('#customerId').val();
        console.log(customerId);
    });
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    
    <!-- Button trigger modal -->
    <ul>
        <li><a data-toggle="modal" data-customer-id="222" data-target="#editCustomerModal">Edit Customer Detail: 222</a></li>
        <li><a data-toggle="modal" data-customer-id="333" data-target="#editCustomerModal">Edit Customer Detail: 333</a></li>
    </ul>
    <!-- Modal -->
    <div id="editCustomerModal" class="modal fade" type="hidden">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    Edit Customer
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>
                <div class="modal-body">
                    <table class="form-table">
                        <tbody>
                        <tr>
                            <td><input type="hidden" id="customerId" path="#" class="form-control" /></td></tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                    class="not-required" for="pool-name">Customer Name:</label></td>
                            <td><input type="text" id="customerName" title="Company Name" path="#"
                                       class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                    class="not-required" for="expire-after">Contact Name:</label></td>
                            <td><input type="text" id="contactName" path="#"
                                       class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                    class="not-required" for="description">Street:</label></td>
                            <td><input type="text" id="street" path="#"
                                       class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                    class="not-required" for="description">State:</label></td>
                            <td><input type="text" id="state" path="#"
                                       class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                    class="not-required" for="expire-after">Zip-Code:</label></td>
                            <td><input type="text" id="zipCode" path="#"
                                       class="form-control" /></td>
                        </tr>
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                    class="not-required" for="expire-after">Country:</label></td>
                            <td><input type="text" id="country" path="#"
                                       class="form-control" /></td>
                        </tr>
    
                        <tr valign="top">
                            <td style="width: 25% !important;"><label
                                    class="not-required" for="expire-after">Email:</label></td>
                            <td><input type="text" id="email" path="#"
                                       class="form-control" /></td>
                        </tr>
    
                        </tbody>
                    </table>
    
    
                </div>
                <div class="modal-footer">
                    <div>
                        <input type="submit" id="createNewCustomer" value="Save"
                               class="btn btn-default" onClick="alert('To be Implemented');" />
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
    
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 感谢您的建议。正如您在上面更新的代码中看到的,我已经替换了您建议的所有代码,我在浏览器或服务器上没有看到任何错误,但模式没有加载客户数据。所有文本框都被加载为空。我是否错过了您建议的任何内容
    • 看看我更新的sn-p。我建议更改添加数据 customerid 的 li 标签。我明天晚上去看看。对不起。谢谢
    • 好的,感谢您的宝贵时间。如您在上面看到的,我已按照您对 li 标签的建议进行了更改。我没有像你一样给出静态值,因为我希望该值是动态的,从数据库中获取。对于每个列出的客户行,都有一个操作并且它具有编辑选项,编辑应该使用该行上的客户 ID 来填充模式上的客户值。
    • 我已经对我的 JS 文件进行了更改,我之前的错误消失了,但出现了一个新错误“ation/edit?id=undefined”,因此设置 id 对我不起作用。请有什么想法,谢谢。
    • 此错误是因为 data-customer-id 在您的第一个模式内的编辑按钮上未定义,或者您没有获得值并将其传递给第二个模式。我假设您对于所有 CRUD 操作只有一种模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2023-03-09
    相关资源
    最近更新 更多