为什么在jQuery的确认窗口中有一个循环?

2cmtqfgy  于 2023-05-06  发布在  jQuery
关注(0)|答案(2)|浏览(61)

为什么在我选择确定或取消后,确认窗口总是立即重新出现?我该怎么弥补?

var originalValue = "";
    $("#betreffDropdown").change(function () {
        originalValue = $("#betreffDropdown option:selected").attr("class");
    });

    $('#nachrichtentext').on('blur', function (e) {
        var currentValue = $('#nachrichtentext').val(); 
        if (currentValue !== originalValue) {
            if (confirm('Du hast ungespeicherte Änderungen. Möchtest du diese Speichern?')) {
                update();
            }
            else {
                $('#nachrichtentext').focus();

            }
        } 
    });

    function update() {
        var selectedId = $("#betreffDropdown option:selected").attr("id");
        var message = $("#nachrichtentext").val();
        var subject = $("#betreffDropdown option:selected").val();
        var data = {
            "selectedId": selectedId,
            "newMessage": message,
            "subject": subject
        };

        $.ajax({
            type: 'GET',
            url: 'updatetemplate/',
            data: data,
            contentType: "application/json",

        })
        window.location.reload()
    }

    var templates = Model.Templates.ToList();
    <select id="betreffDropdown" name="BetreffDropdown">
    <option>Bitte wählen:</option>
    @if (Model.Templates != null)
    {

            foreach (var template in templates.Distinct())
        {
            <option id="@template.Item1" class="@template.Item2">@template.Item3</option>
        }
    }
</select>
}
@using (Html.BeginForm("CreateView", "Template", null))
{
    <button>Neu</button>
}

    <textarea  readonly id="nachrichtentext" placeholder="Nachricht"></textarea>

    <input type="checkbox" id="anpassen" name="Anpassen" disabled>

    <label for="anpassen">Anpassen</label>
    <br />
    <br />
<button id="savebtn" disabled class="disabled-button" onclick="window.location.reload()">Speichern</button>

<button id="deletebtn" class="disabled-button" onclick="window.location.reload()">Löschen</button>

这是我使用的代码。因此,我检查文本区域的值是否已更改,如果是,并且文本区域失去焦点,则会出现一个确认窗口。单击“确定”应更新模板,否则焦点应返回到文本区域。
在我的研究中,我什么也没发现……

u91tlkcl

u91tlkcl1#

如果没有一个完整的功能示例,则很难判断。但这里有一些可能的错误:

  • js的多重包含。可能执行$('#nachrichtentext').on('blur',的脚本被加载了不止一次。
  • 多个事件,因为 AJAX 响应。如果你有一个 AJAX 响应执行$('#nachrichtentext').on('blur',,那么这个响应可能不止一次给出,导致在blur事件中触发了多个等效的处理函数。
  • 递归调用,update()正在进行一些递归,再次手动或通过操纵DOM元素上的焦点触发模糊。
xzlaal3s

xzlaal3s2#

我想明白了

setTimeout(function () {
    $('#nachrichtentext').focus();
}, 0);

我有0毫秒的超时代码。idk它是如何工作的,但它的工作

相关问题