简体   繁体   English

Modal不会关闭并阻止默认情况下的无点击操作,并且不会在yes单击时执行func

[英]Modal won't close and prevent default on no click, and won't execute func on yes click

I have a modal with two buttons. 我有两个按钮的模态。 One is a yes button and one is a no button. 一个是“是”按钮,一个是“否”按钮。 If yes button is pressed, I would like the remainder of the function to execute. 如果按“是”按钮,我希望其余功能执行。 If no btn clicked, I would like the page to prevent default and not execute. 如果未单击btn,我希望页面阻止默认设置并且不执行。 However, it seems that regardless of which button is clicked, nothing happens besides the modal closing. 但是,似乎无论单击哪个按钮,除了模式关闭之外什么都没有发生。 I am using a modal example I found elsewhere, so this may be the issue. 我正在使用在其他地方找到的模式示例,所以这可能是问题所在。 After glancing at it for some time I cannot seem to find where the issue is. 看了一段时间后,我似乎找不到问题所在。 Am I missing something small? 我想念一些小东西吗? Or perhaps my Jquery is wrong? 还是我的Jquery错误? Below is my code: 下面是我的代码:

Modal: 莫代尔:

    <!-- Modal for delete-->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content border-primary mb-3 box-shadow-none img-responsive">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="card-body bg-light">
                <div id="del" class="center">
                    <label>Are you sure you want to delete?</label>
                </div>
            </div>
            <div class="modal-footer">
                <div id="deleteYes">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="deleteYes">Yes</button>
                </div>
                <div id="deleteNo">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="deleteNo">No</button>
                </div>
            </div>
        </div>
    </div>
</div>

And here is my Jquery: 这是我的Jquery:

 $(".btnDeleteTerminalCommand").click(function (e) {

    $("#deleteModal").modal('toggle');

    if ($("#deleteNo").click) {
        return e.preventDefault();
    }

    var rowId = "#" + $(this).data("rowindex");
    var row = $(rowId);
    var termId = row.find(".tdTermId").html().trim();
    var cmdId = row.find(".tdCmdId").html().trim();
    var cmdVal = row.find(".tdCmdVal").html().trim();
    var cmdID = row.find(".cmdID").html().trim();

    var data = {
        TerminalID: termId,
        CommandID: cmdId,
        CommandValue: cmdVal,
        ID: cmdID
    };

    $.ajax({
        url: '@Url.Action("DeleteTerminalCommand", "TerminalCommand")',
        type: "POST",
        data: data,
        success: function (response) {
            console.log("Success");
            window.location.href = response.Url;
        }
    });
});

Any advice helps! 任何建议都会有所帮助! Thank you! 谢谢!

Your click handler will toggle the modal and immediately continue to execute the rest of the function before the user can click anything. 您的点击处理程序将切换模式,并立即继续执行其余功能,然后用户才能单击任何内容。 If your modal has two buttons, create a click handler for each button. 如果您的模式有两个按钮,则为每个按钮创建一个单击处理程序。 Perhaps the No button just closes the modal. 也许“否”按钮只是关闭模式。 The Yes button handler can execute the actions required to accomplish the task. 是按钮处理程序可以执行完成任务所需的操作。

$(".btnDeleteTerminalCommand").click(function(e){
    $("#deleteModal").modal('toggle');
}

$("#deleteNo").click(function(e){
    $("#deleteModal").modal('hide');
}

$("#deleteYes").click(function(e){
    // build data object
    // ajax post
}

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

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