简体   繁体   English

区分 Bootstrap Modal 中的按钮

[英]Tell the difference between buttons in a Bootstrap Modal

I'm using bootstrap to make a quick throw-together website for a little project I'm working on.我正在使用引导程序为我正在进行的一个小项目制作一个快速的网站。

I'm using a Modal for users to confirm an action and I'm not sure how to tell the difference between the buttons being pressed within the modal.我正在为用户使用模态来确认操作,但我不确定如何区分模态中按下的按钮之间的区别。 There is a delete button and a cancel button.有一个删除按钮和一个取消按钮。

Code:代码:

$('.deleteButton').click((event) => {

                $('#confirmDeleteSingleInfractionModal').modal('toggle');

                let data = {
                    infractionID: $(event.target).attr('referencedInfraction'),
                    targetUserID: $(event.target).attr('targetUserID')
                }


                console.log(data);


                $('#confirmDeleteSingleInfractionModal').on('hide.bs.modal', function (close) {

                    
                    if ($(close.relatedTarget).hasClass('cancelButton'))
                        return;



                    console.log('deleting');

                    $.ajax({
                        type: 'delete',
                        url: `${window.location.origin}/dataLink/infraction`,
                        data,
                        success: () => {

                        },
                        error: () => {
                            showInfractionDeleteError();
                        }
                    })

                })
            });

I gave all the cancel buttons a cancelButton class so I could use jQuery to check if it was a cancel button.我给所有的取消按钮一个cancelButton class 所以我可以使用 jQuery 来检查它是否是一个取消按钮。

However, I've come to notice that it still doesn't return even if it has the class (the button code is below)但是,我注意到即使它有 class(按钮代码在下面),它仍然没有返回

<div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-x-circle" viewBox="0 0 16 16">
                        <path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
                        <path d="M4.646 4.646a.5.5 0 0 1 .708 0L8 7.293l2.646-2.647a.5.5 0 0 1 .708.708L8.707 8l2.647 2.646a.5.5 0 0 1-.708.708L8 8.707l-2.646 2.647a.5.5 0 0 1-.708-.708L7.293 8 4.646 5.354a.5.5 0 0 1 0-.708z"/>
                    </svg> Delete </button>
                <button type="button" class="cancelButton btn btn-primary" data-dismiss="modal">Cancel</button>
            </div>

If anybody has any suggestions as to how I could make it return when the button pressed has the cancelButton class or any other way to do what I'm trying to do, that would be very appreciated.如果有人对我如何在按下的按钮有cancelButton class 或任何其他方式来做我想做的事情时让它返回有任何建议,那将非常感激。

Thanks in advance.提前致谢。

On hide.bs.modal , the event provided in function (close) { is not the same as the one in $('.deleteButton').click((event) => { .hide.bs.modal中, function (close) {中提供的事件与$('.deleteButton').click((event) => {中的事件不同。

In your code, every time the .deleteButton is clicked, the modal display is toggled... And a new hide.bs.modal event listener is registered (not executed right away)... But milliseconds after, the modal toggling fires the hide.bs.modal ... Making you think it is directly related to the .deleteButton click... Which is wrong.在您的代码中,每次单击.deleteButton时,都会切换模式显示...并且注册了一个新的hide.bs.modal事件侦听器(不会立即执行)...但是几毫秒后,模式切换会触发hide.bs.modal ...让您认为它与.deleteButton click 直接相关...这是错误的。

That can be confusing, I agree.这可能会令人困惑,我同意。

Just assume that registering an event handler inside another event handler is wrong 99% of the time.假设在另一个事件处理程序中注册一个事件处理程序在 99% 的情况下都是错误的。

What is inside the hide.bs.modal event handler execute every time the modal closes.每次模态关闭时都会执行hide.bs.modal事件处理程序中的内容。 Whatever the close reason.不管是什么原因。 If you console.log close.relatedTarget ... You would notice it isn't a reference to the .deleteButton button.如果你 console.log close.relatedTarget ...你会注意到它不是对.deleteButton按钮的引用。 It probably is squarely undefined (I did not check).它可能是完全undefined的(我没有检查)。

So here is my suggestion:所以这是我的建议:

$('.deleteButton').click((event) => {
    
    // Something to do when a .deleteButton is clicked
    
    $('#confirmDeleteSingleInfractionModal').modal('toggle');

    let data = {
        infractionID: $(event.target).attr('referencedInfraction'),
        targetUserID: $(event.target).attr('targetUserID')
    }

    console.log(data);
    console.log('deleting');

    $.ajax({
        type: 'delete',
        url: `${window.location.origin}/dataLink/infraction`,
        data,
        success: () => {

        },
        error: () => {
            showInfractionDeleteError();
        }
    })
});

$('.cancelButton').click((event) => {

  // Something to do when a .cancelButton is clicked
  // ...
})

$('#confirmDeleteSingleInfractionModal').on('hide.bs.modal', function (close) {

  // Something else to do on modal close (Whatever the close reason)
  // ...
})

the

$('#confirmDeleteSingleInfractionModal').on('hide.bs.modal', function (close) {...}

is nested inside the嵌套在

$('.deleteButton').click((event) {...}

so i would take it out of there.所以我会把它从那里拿出来。

Second, provide two different event handlers for the two different buttons (cancel and delete) and handle the action you wish in each event handler.其次,为两个不同的按钮(取消和删除)提供两个不同的事件处理程序,并在每个事件处理程序中处理您希望的操作。 In order to tell the difference between the buttons being pressed within the modal you can provide two different classes for each button.为了区分模态中按下的按钮之间的区别,您可以为每个按钮提供两个不同的类。 let's apply "cancel-button" class for the cancel button and "delete-button" for the delete button.让我们为取消按钮应用“取消按钮”class,为删除按钮应用“删除按钮”。

$('.cancel-button').click((event) => {
     // cancel action
})

$('.delete-button').click((event) => {
     // delete action
})

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

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