简体   繁体   English

按钮单击事件在 Bootstrap 3 模态中多次触发

[英]button click event fires multiple times inside of a Bootstrap 3 Modal

I have a bug (or feature) whereby any button that is inside a Bootstrap 3 modal will fire the click event several times.我有一个错误(或功能),即 Bootstrap 3 模式内的任何按钮都会触发点击事件多次。 Besides going "old-school" and calling a script from an HTML button directly (which always works), is there a workaround for this?除了去“老派”并直接从 HTML 按钮调用脚本(始终有效)之外,是否有解决方法?

                    <div class="modal-footer">
                        <span id="spanSendReport">
                            <button type="button" id="btnSendReport" class="btn btn-success" data-dismiss="modal">Send Report</button></span>
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>

And the jquery和 jquery

$("#btnSendReport").click(function (e) {
... my code
});

Using Bootstrap v3.4.1 and jQuery v3.3.1使用 Bootstrap v3.4.1 和 jQuery v3.3.1

Never ran into this before (except with things posting back, but there are no postbacks of any kind happening - checked in the browser and in debugger).以前从未遇到过这种情况(除了发回的东西,但没有发生任何形式的回发 - 在浏览器和调试器中检查)。 There is only 1 button in the DOM with the Id (first thing I checked), but it fires the click event 4 times, every time? DOM 中只有一个带有 Id 的按钮(我检查的第一件事),但它每次都会触发点击事件 4 次? Any ideas有任何想法吗

NOTE: I forgot to mention, the modal is opened from another modal.注意:我忘了提,模态是从另一个模态打开的。 Maybe that has something to do with it.也许这与它有关。

The complete code inside the click event function (just in case it has something to do with it):点击事件 function 中的完整代码(以防万一它与它有关):

$("#btnSendReport").click(function (e) {
        var parElem = $(this).parent().parent().parent();
        var listID = parElem.find("#hidLID").val();
        var problemTypes = $.map(parElem.find('option:selected'), function (e) { return e.value; }).join(',');
        var problemTypeOther = parElem.find("#txtProblemTypeOther").val();

        var obj = {};
        obj.lid = listID;
        obj.ProblemTypes = problemTypes;
        obj.ProblemTypeOther = problemTypeOther;

        try {
            $.ajax({
                type: "POST",
                url: "../../api/reportdiscrepancy/",
                data: obj,
                dataType: "json",
                success: function (data) {
                    var result = data;
                },
                error: function (err) {
                    console.log(err);
                }
            });
        } catch (err) {
            console.log(err);
        }
    });

Found the problem and the answer to many issues I've been running into before and the culprit is function pageLoad() which is what older ASP.Net webforms uses for client side actions.找到了我之前遇到的许多问题的问题和答案,罪魁祸首是function pageLoad()这是旧的 ASP.Net 网络表单用于客户端操作的方法。 I removed the '#btnSendReport' click to OUTSIDE of the pageLoad() function and it's only firing once now.我删除了“#btnSendReport”点击到 pageLoad() function 外部的点击,它现在只触发一次。

It looks like any MS-AJAX accumulates ALL event bindings (not actually firing them) within an UpdatePanel, then a final call to a click event (or whatever) will fire x number of times.看起来任何 MS-AJAX 都会在 UpdatePanel 中累积所有事件绑定(实际上并未触发它们),然后对单击事件(或其他)的最终调用将触发 x 次。

I was clued in as I made 3 changes to get to the modal and the last thing I did was click on the button (so it fired 4 times).当我进行 3 次更改以进入模式时,我被提示了,我做的最后一件事是单击按钮(所以它触发了 4 次)。 I believe it may have to do with the default setting of the UpdatePanel with ChildrenAsTriggers=True (unfortunately, I have to leave that on as other functionality breaks if set to false).我相信它可能与 ChildrenAsTriggers=True 的 UpdatePanel 的默认设置有关(不幸的是,如果设置为 false,我必须将其保留为其他功能中断)。

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

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