简体   繁体   English

使用 Jquery Ajax 刷新模态内容

[英]Refreshing Modal content with Jquery Ajax

I'm having issues with getting a form loaded into a modal in BS4 to repost (post to self in affect), check the form input, and re-load the modal div with the error, or re-direct to checkout page.我在将表单加载到 BS4 中的模态以重新发布(发布到自我影响)、检查表单输入并重新加载带有错误的模态 div 或重定向到结帐页面时遇到问题。

The initial Ajax loading of the form from select.php loads well, but upon pressing the submit button on the form within select.php in the modal, I just get no response at all from the button, as if the button it dead, nothing shown in console etc.来自 select.php 的表单的初始 Ajax 加载加载良好,但是在模式中的 select.php 中的表单上按下提交按钮时,我根本没有收到按钮的响应,好像按钮已经死了,什么都没有显示在控制台等中。

Any ideas at all?有什么想法吗? I'm new to using JQ and Ajax etc, but it seems quite close to what other people are doing in their code.我是使用 JQ 和 Ajax 等的新手,但它似乎与其他人在他们的代码中所做的非常接近。

    <script>
    $(document).ready(function() {
        $('.view_data').click(function() {
            var eventID = $(this).attr("id");
            $.ajax({
                url: "select.php",
                method: "post",
                data: {
                    eventID: eventID,
                    selectToken: 'true'
                },
                success: function(data) {
                    $('#modal-body').html(data);
                    $('#ticketsModal').modal("show");
                }
            });
        });



        // process the form in select.php shown in the modal
        $('#ticketForm').submit(function(event) {

            // get the form data
            // there are many ways to get this data using jQuery (you can use the class or id also)
            var formData = {
                'maleplaces': $('input[name=maleplaces]').val(),
                'femaleplaces': $('input[name=femaleplaces]').val(),
                'friendplaces': $('input[name=friendplaces]').val(),
                'eventID': $('input[name=eventID]').val(),
                'ticketSubmit': $('input[name=ticketSubmit]').val()
            };


            $.ajax({
                    type: 'POST', 
                    url: 'select.php', 
                    data: formData, 
                    dataType: 'json', 
                    encode: true
                })
                // using the done promise callback

                .done(function(data) {
                    $('#modal-body').html(data);
                    console.log(data);

                    // here we will handle errors and validation messages
                });

            // stop the form from submitting the normal way and refreshing the page
            event.preventDefault();
        });


    });
</script>

The Modal code on the main.php page is here as requested: main.php 页面上的 Modal 代码如下:

    <!-- Modal -->
<div class="modal fade" id="ticketsModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Select </h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" id="modal-body">
                ...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<!-- Modal End-->

Here is the form in select.php:这是 select.php 中的表单:

            <form id="ticketForm" name="ticketForm" method="post" action="select.php">

                <div class="form-group">
                    <select name="maleplaces" id="maleplaces" class="custom-select">
                        <option value="0" selected>Male Tickets (0)</option>
                        <option value="1">Male Tickets (x1)</option>
                        <option value="2">Male Tickets (x2)</option>
                        <option value="3">Male Tickets (x3)</option>
                        <option value="4">Male Tickets (x4)</option>
                        <option value="5">Male Tickets (x5)</option>
                    </select>
                </div>

                <div class="form-group">
                    <select name="femaleplaces" id="femaleplaces" class="custom-select">
                        <option value="0" selected>Female Tickets (0)</option>
                        <option value="1">Female Tickets (x1)</option>
                        <option value="2">Female Tickets (x2)</option>
                        <option value="3">Female Tickets (x3)</option>
                        <option value="4">Female Tickets (x4)</option>
                        <option value="5">Female Tickets (x5)</option>
                    </select>
                </div>


            <div class="form-group">
                <select name="friendplaces" id="friendplaces" class="custom-select">
                    <option value="0" selected>Double Tickets (0)</option>
                    <option value="1">Saver Tickets (x1)</option>
                    <option value="2">Saver Tickets (x2)</option>
                    <option value="3">Saver Tickets (x3)</option>
                    <option value="4">Saver Tickets (x4)</option>
                    <option value="5">Saver Tickets (x5)</option>
                </select>

                <input name="eventID" type="hidden" id="eventID" value="<?php echo $event['event_id']; ?>" />
                <input name="ticketSubmit" type="hidden" id="ticketSubmit" value="<?php echo $event['event_id']; ?>" />

            </div>
    </div>

    <div class="col-auto my-1">
        <button type="submit" id="ticket_btn" class="btn btn-primary" onclick="return doubleticketFunction()">Continue to payment</button>
    </div>

    </form>

Because you dynamically change your #ticketForm element you lose the bind with the submit event listener when you replace your form.因为您动态更改了#ticketForm元素,所以当您替换表单时,您将失去与submit事件侦听器的绑定。 You'll have to either rebind the event or use event delegation to listen for the events.您必须重新绑定事件或使用事件委托来侦听事件。 In this example I've chosen the former to elaborate.在这个例子中,我选择了前者来详细说明。

First take your function that you use in the $('#ticketForm').sumbit and name it.首先获取您在$('#ticketForm').sumbit使用的函数并命名它。 You'll be using this function to bind the first form and all the forms that replace it.您将使用此函数绑定第一个表单和所有替换它的表单。

function ticketFormSubmit(event) {

  // Use jQuery serialize to get the form values.
  var formData = $(this).serialize();

  $.ajax({
    type: 'POST', 
    url: 'select.php', 
    data: formData, 
    dataType: 'json', 
    encode: true
  })
  .done(function(data) {
    $('#modal-body').html(data);
    console.log(data);
  });

  event.preventDefault();
}

In the succes callback of your $.ajax call select the newly added form and add the event listener to it like in the example below.$.ajax调用的成功回调中,选择新添加的表单并向其添加事件侦听器,如下例所示。

$('.view_data').click(function()     
  var eventID = $(this).attr("id");
  $.ajax({
    url: "select.php",
    method: "post",
    data: {
      eventID: eventID,
      selectToken: 'true'
    },
    success: function(data) {
      $('#modal-body').html(data);
      $('#ticketsModal').modal("show");

      // Bind new submit event listener
      $('#ticketForm').on('submit', ticketFormSubmit);
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM