繁体   English   中英

当用户单击提交时,我的 AJAX 没有发布或响应

[英]My AJAX isn't posting or responding when the user clicks submit

我正在尝试列出事件列表,并且在每个事件上都有一个复选框和斧头框,因此一个用于将事件标记为完成,另一个用于将其标记为已删除。 我希望在不刷新页面的情况下发生这种情况,并且每次单击按钮时都没有任何反应。

我认为导致问题的原因是我可能有超过 1 个事件框,它们不是唯一的,但我不确定如何确保它们是唯一的,以便在我的数据库中更新正确的事件。 我确实有单独的文件,但它们只包含我的查询,我遇到的问题是它甚至没有发布数据。 当我单击该框时,它什么也不做。

HTML/PHP

                <div class="w3-card w3-round w3-white w3-center delete_mem">
              <div class="w3-container">
                <p>Upcoming Event</p>
                <!-- <img src="/w3images/avatar6.png" alt="Avatar" style="width:50%"><br> -->
                <span><?= $appRow['customer_appointments_type']; ?></span>
                <p><?= $appRow['customer_appointments_datetime']; ?></p>
                <div class="w3-row w3-opacity">
                  <div class="w3-half">
                    <!-- <button class="w3-button w3-block w3-green w3-section" title="Complete"><i class="fa fa-check"></i></button> -->
                    <a id="<?= $appRow['customer_appointments_id']; ?>" class='w3-button w3-block w3-green w3-section btn-good'><i class='fa fa-check'></i></a>
                  </div>
                  <div class="w3-half">
                    <!-- <button class="w3-button w3-block w3-red w3-section" title="Delete"><i class="fa fa-remove"></i></button> -->
                    <a id="<?= $appRow['customer_appointments_id']; ?>" class='w3-button w3-block w3-red w3-section btn-danger'><i class='fa fa-remove'></i></a>
                  </div>
                </div>
              </div>
            </div>

Javascript/AJAX

            //Mark as deleted without refreshing the page
        $(document).ready(function() {
            $('.btn-danger').click(function() {
                var id = $(this).attr("id");
                if (confirm("Are you sure you want to delete this appointment?")) {
                    $.ajax({
                        type: "POST",
                        url: "scripts/lead/deleteLeadTask.php",
                        data: ({
                            id: id
                        }),
                        cache: false,
                        success: function(html) {
                            $(".delete_mem" + id).fadeOut('slow');
                        }
                    });
                } else {
                    return false;
                }
            });
        });

        //Mark as complete without refreshing the page
        $(document).ready(function() {
            $('.btn-good').click(function() {
                var id = $(this).attr("id");
                if (confirm("Are you sure you want to complete this appointment?")) {
                    $.ajax({
                        type: "POST",
                        url: "scripts/lead/completeLeadTask.php",
                        data: ({
                            id: id
                        }),
                        cache: false,
                        success: function(html) {
                            $(".delete_mem" + id).fadeOut('slow');
                        }
                    });
                } else {
                    return false;
                }
            });
        });

首先,正如您所提到的,您不应该有具有相同 ID 的不同元素。 将您的代码更改为这样的内容

Html

<button type="button" onClick='delete(<?= $appRow['customer_appointments_id']; ?>)' class='w3-button w3-block w3-red w3-section btn-danger'><i class='fa fa-remove'></i></button>

然后对于 Javascript,编写 function,当有人单击删除按钮时将调用它

Javascript

function delete(id){ if (confirm("Are you sure you want to delete this appointment?")) { $.ajax({ type: "POST", url: "scripts/lead/deleteLeadTask.php", data: ({ id: id }), cache: false, success: function(html) { $(".delete_mem" + id).fadeOut('slow'); } }); } else { return false;} }

暂无
暂无

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

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