【问题标题】:display id button of popup doesn't function弹出窗口的显示 id 按钮不起作用
【发布时间】:2018-08-14 10:39:31
【问题描述】:

我有一个显示弹出窗口的按钮。我需要知道弹出窗口关闭后单击的按钮的 ID。警报显示没有 id 如何从第一个函数传递值 ID 以在第二个函数中使用它?

html 中带有要显示的弹出窗口的按钮:

<p class="stop_inv">
<span class="glyphicon glyphicon-remove-sign" aria-hidden="true"></span>
Stop</p>

同一个html中的弹窗:

<div class="modal fade" id="confirm-stop-automated-popup" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
<div class="modal-dialog" style="width:370px;" role="document">
<div class="modal-content">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
     <form class="form-horizontal" method="post" id="stop_automated">
<div class="modal-body">
     <div class="row">
        <h1 class="center" style="font-size:100%;">
            Do you wish to close them immediately ?
        </h1>
    </div>
    </div>
<div class="modal-footer">

    <button type="button" class="btn btn-primary" name="ok" data-dismiss="modal" id="confirm-stop-button">Ok</button>
    <button type="button" class="btn btn-primary" data-dismiss="modal" id="cancel-delete-button">Cancel</button>
  </div>
  </form>
</div>
</div>
</div>

我在 javascript 中的函数:(显示了弹出窗口,但问题是我不知道点击了弹出窗口中的哪个按钮,应该在第二个函数中传递 var 信息以使用它。

$('.automated-orders-list .stop').on('click', function() {
var $button = $(this);
var info = $button.closest('.data').attr('data-order-id');
    $('#confirm-stop-automated-popup').modal('show');
    e.preventDefault();
    return;
});

第二个弹出窗口知道点击了哪个按钮不起作用:

$('#confirm-stop-automated-popup .modal-footer button').on('click', function(event) {

    var $button = $(event.target); // The clicked button

    $(this).closest('.modal').one('hidden.bs.modal', function() {
        // Fire if the button element 
        alert('The button that closed the modal is: ', $button);
    });

});

【问题讨论】:

    标签: javascript jquery html django


    【解决方案1】:

    如果您的主要目标只是显示点击了哪个按钮,您可以使用下面的代码来实现此目的。

    $('#confirm-stop-automated-popup .modal-footer button').on('click',
        function() {
            console.log('The button that closed the modal is: ' + this.innerText);
            //alert('The button that closed the modal is: ' + this.innerText);
        });
    

    当您单击一个按钮时,您已经持有该按钮对象。通过this 关键字,您可以读取该对象的属性。您无需通过event.target 显式查找目标按钮。

    $('#confirm-stop-automated-popup .modal-footer button').on('click',
        function(event) {
            var $button = this.innerText;
    
            $(this).closest('.modal').one('hidden.bs.modal', function() {
                // Fire if the button element 
                console.log('The button that closed the modal is: ' + $button);
            });
    
        });
    

    【讨论】:

    • 非常感谢您的帮助!但是我怎么知道点击的按钮的ID?
    • this.id 将给出按钮 ID。在我写 innerText 的地方用 id 替换它。
    • 欢迎。如果我的回答解决了您的问题,请单击大复选框接受它作为答案。这将帮助我们(贡献者)建立一个回购。在这个社区中。
    • 当然! .. 我还有一个问题,非常感谢您的帮助。我有一个名为 info 的变量,我需要将它传递给第二个函数来使用它。我怎样才能做到这一点?问题已编辑感谢您的宝贵时间!
    • 因为它是一个局部变量,您可以在第二个 func() 中复制粘贴相同的内容。如果您想重用相同的,请改为全局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    相关资源
    最近更新 更多