编程语言
首页 > 编程语言> > 如何在模态内单击按钮时使用jQuery或javascript获取引导模式的数据值?

如何在模态内单击按钮时使用jQuery或javascript获取引导模式的数据值?

作者:互联网

我想在模态内的单击按钮上获取引导模式的数据值.
这是我的模态 –

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                                    <h4 class="modal-title" id="myModalLabel">Cancel</h4>
                                </div>
                                <div class="modal-body"> 
                                    Are you sure you want to cancel this?
                                </div>
                                <div class="modal-footer">
                                    <button type="button" id="savebutton" class="btn btn-success" >Yes</button>  
                                    <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
                                </div>
                            </div>
                        </div> 
                    </div>

以下是我将数据传递给模态的方式 –

更新 –

    <button data-target='#myModal' id='link' data-toggle='modal' data-id="1"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 1</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="2"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 2</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="3"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 3</button>
    ...
    ..
    .
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="n"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel n</button>

可以从众多按钮中的任何一个调用模态,我只需要获取相关按钮的数据ID.例如 – 如果我点击“取消1”按钮,我会在点击模态中的“是”按钮后将数据ID设为1.

我想获取“id”字段的模态数据值,使用jQuery或javascript在此模式中单击“是”按钮时为1234.

最佳答案:

我修改了你的小提琴:
http://jsfiddle.net/exr00e0d/

你有过href.而不是我采取目标(模态).

$('.cancel_modal').click(function() {
   //alert('called');
    // we want to copy the 'id' from the button to the modal
    var href = $(this).data('target');
    var id = $(this).data('id');

    // since the value of href is what we want, so I just did it like so
    alert(href);
    // used it as the selector for the modal
    alert(id);
    $(href).data('id', id);
});

标签:jquery,javascript,twitter-bootstrap,bootstrap-modal
来源: https://codeday.me/bug/20190516/1114799.html