gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:11:47 25 4
gpt4 key购买 nike

我想通过单击模态内的按钮来获取 Bootstrap 模态的数据值。这是我的模式 -

<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>

Modal 可以从许多按钮中的任何一个调用,我只需要获取相关按钮的数据 ID。例如-如果我点击“取消 1”按钮,我应该在点击模式中的"is"按钮后获得数据 ID 为 1。

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

最佳答案

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

关于javascript - 如何在模态内单击按钮时使用 jQuery 或 javascript 获取 Bootstrap 模态的数据值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40107269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com