简体   繁体   English

有没有办法在document.ready()中获取引导程序模式的e.relatedTarget?

[英]Is there a way to get e.relatedTarget of bootstrap modal in document.ready ()?

I have two buttons that invoke the same modal, I have set data-button attribute to detect which button invoked the modal. 我有两个调用相同模式data-button ,我设置了data-button属性以检测哪个按钮调用了模式。

<button type="button" id="another" data-toggle="modal" data-target="#modal" class="btn btn-warning" data-button='another'>Add another</button>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal" data-button="lab">Add  lab</button>

what I wanted to do is to change a label inside a modal depending on the button invoker. 我想要做的是根据按钮调用程序在模式内部更改标签。 I did so : 我是这样做的:

<script>
$('#modal').on('show.bs.modal', function(e) {
    var $trigger = $(e.relatedTarget);
    $('#vv_type').val($trigger.data('button'));
    v_type = $trigger.data('button');
    if (v_type == 'lab') {
        $('#ModalLabel').html('changed label');
        $("label[for='id_date_of_label']").html('label inside modal changed');
    }
})
</script>

it works as expected only the first time when modal is shown , and after that the same changed label is always displayed whenever I click on the buttons, and this is logic due to $('#modal').on('show.bs.modal', function (e) { 它仅在第一次显示modal时才按预期工作,然后每次单击按钮时总是显示相同的更改标签,这是由于$('#modal').on('show.bs.modal', function (e) {

Is there a way to declare v_type variable and send it to document.ready() function so that I make sure that the script will run every time I click on buttons invoking the modal (not only the first time?) 有没有一种方法可以声明v_type变量并将其发送到document.ready()函数,以确保每次单击调用模式的按钮时(不仅是第一次),脚本都将运行。

I have solved this by getting the data-button from a hidden input vv_type on the modal (my modal has already a form in it ) and I added this script which is ran each time a modal is shown: 我已经通过从模态上的隐藏输入vv_type获取data-button解决了这个问题(我的模态中已经有一个形式),并且我添加了此脚本,该脚本在每次显示模态时都运行:

 <script>
    $(document).ready(function () {
        $('.modal').on('show.bs.modal', function (e) {
            if ($('#vv_type').val() == 'lab') {
                 $('#ModalLabel').html('changed label');
    $("label[for='id_date_of_label']").html('label inside modal changed');
            }
            else {
                 $('#ModalLabel').html('origin label');
    $("label[for='id_date_of_label']").html('label inside modal origin');
            }
        });
    });
</script>

Your original code still works for me. 您的原始代码仍然对我有用。 The only part missing is the else condition for if (v_type == 'lab') { . 唯一缺少的部分是if (v_type == 'lab') {else条件。 I can see the label change on each button click. 我可以看到每次单击按钮时标签都会发生变化。

 $('#myModal').on('show.bs.modal', function (e) { var $trigger = $(e.relatedTarget); //$('#vv_type').val($trigger.data('button')); v_type = $trigger.data('button'); if (v_type == 'lab') { $('#buttonText').html(v_type); //$("label[for='id_date_of_label']").html('label inside modal changed'); } else { $('#buttonText').html(v_type); //$("label[for='id_date_of_label']").html('label inside modal changed'); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <button type="button" data-button="lab" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <button type="button" data-button="lab 123" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button> <div id="myModal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <p id="buttonText"></p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> 

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

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