如何在模态内单击按钮时使用 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。

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

原文由 gauravparmar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 227
2 个回答

我修改了你的小提琴: 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);
});

原文由 Abdul Rehman Sayed 发布,翻译遵循 CC BY-SA 3.0 许可协议

在 jQuery 中

$('#savebutton').click(function() {
    var id = $('#link').data('id');
    // if for some reason, the code above doesn't work,
    // $('#link').attr('data-id');
    console.log(id);
});

编辑:

看着 你的 jsfiddle

 $('#link').click(function() {
    // we want to copy the 'id' from the button to the modal
    var target = $(this).attr('target');
    var id = $(this).data('id');

    $(target).data('id', id);
});

$('#savebutton').click(function() {
    // now we grab it from the modal
    var id = $('#addBookDialog').data('id');

    console.log(id);
});

原文由 Craftein 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题