gpt4 book ai didi

jquery - stopPropagation 阻止显示 Bootstrap 的对话框

转载 作者:行者123 更新时间:2023-12-01 03:36:37 25 4
gpt4 key购买 nike

我在 div 中有一个按钮

<div id="outerDiv">
<button data-details-rid="@Model.RequestId" data-toggle="modal" data-target="#showRequestModal">Details</button>
</div>

引导的模式对话框

   <div id="showRequestModal" 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>
<div id="request-details-title">Details</div>
</div>
<div id="request-details-modal-body" class="modal-body">
</div>
</div>
</div>
</div>

以及运行点击事件的 jquery 函数:

    $("[data-details-rid]").on('click', function (event) {
var request_id = $(this).attr('data-details-rid');
console.log(request_id);
var request_details = {};
request_details.url = "/Requests/Details?RequestId=" + request_id;
request_details.async = false;
request_details.datatype = "html";
request_details.contentType = "application/json; charset=utf-8";
request_details.success = function (request_info) {/*...*/};
$.ajax(request_details);
});

$("#outerDiv").on('click', function (event) {
another ajax call
});

现在,显然,当我单击按钮时,#outerDiv 函数也会被调用(不良影响)。

当我这样放置event.stopPropagation()时:

$("[data-details-rid]").on('click', function (event) {
event.stopPropagation();
var request_id = $(this).attr('data-details-rid');
console.log(request_id);
var request_details = {};
request_details.url = "/Requests/Details?RequestId=" + request_id;
request_details.async = false;
request_details.datatype = "html";
request_details.contentType = "application/json; charset=utf-8";
request_details.success = function (request_info) {/*...*/};
$.ajax(request_details);
});

然后模态对话框不会出现。为什么?

最佳答案

这是预期的行为。通过使用 stopPropagation ,您基本上是说不再有点击事件处理程序会收到该事件的通知,就像您在 documentation 中读到的那样。 。但是,您可以使用以下命令手动触发模式:$("#showRequestModal").modal('show');

关于jquery - stopPropagation 阻止显示 Bootstrap 的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296638/

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