简体   繁体   English

jQuery事件触发一次,然后不再触发

[英]jQuery event fires once, then never again

I've been wrestling with a simple JQuery event handler for hours. 数小时来,我一直在与一个简单的JQuery事件处理程序搏斗。 My event handler fires exactly once, when the page loads, and never again no matter the event or the interaction with the select box. 当页面加载时,我的事件处理程序仅触发一次,无论事件或与选择框的交互都不会触发。 When deferred, the alert (when I have one) shows the first select option. 延迟后,警报(如果有)将显示第一个选择选项。 When not, the alert is blank. 否则,警报为空白。

All I want is for the select box to load from AJAX and for a user choice to trigger another AJAX call. 我想要的只是从AJAX加载选择框,以及让用户选择触发另一个AJAX调用。

HTML: HTML:

<select id="connection" name="Connection"></select>
<div id="testme" style="background: #CCC; width:100%; height:50px;position:relative;color:red">testing</div>

Javascript: 使用Javascript:

$(document).ready(function () {
    // Event handler. Tried as a separate function and as a parameter to $.on(...)
    function connectionSelected() {
        var str = $('#connection option:selected').text();
        alert(str);
        $("#testme").text(str);
    }

    var $connectionSelect = $('#connection');
    //$connectionSelect.selectmenu(); // Tried enabling/disabling

    // Tried this and all JS code inside and outside of $(document).ready(...)
    $.when(
    $.ajax({
        dataType: "JSON",
        url: '@Url.Content("~/API/ConnectionHint")', // The AJAX call (using ASP Razor) works fine
        success: function(data) {
            // This function is always called and works
            var items = [];
            $.each(data, function(key, val) {
                items.push("<option value='" + key + "'>" + val + "</option>");
            });
            $connectionSelect.append(items.join(""));
            // Tried setting up the event handler here
        },
        error: function() {
            $connectionSelect.html('<option id="-1">none available</option>');
        }
    })
    ).then(function() {
        //$("#connection option").blur(connectionSelected()).change();
        $("#connection").on("change", connectionSelected());
    });
});

Tried dozens of variations of the event handler, several events, inside and outside of a deferred.done and deferred.then, etc.. Eg: 在deferred.done和deferred.then等内部和外部尝试了事件处理程序的多种变体,几个事件。例如:

$connectionSelect.selectmenu({
    change: function (event, data) {
        $('#connection').change(function () {
            var str = "";
            $('#connection').each(function () {
                str += $(this).text() + "<br>";
            });
            $("#testme").text(str);
        });
    }
});

I usually write back-end code and am familiar only with portions of JQuery, and this is driving me crazy. 我通常编写后端代码,并且只熟悉JQuery的某些部分,这使我发疯。 I've looked more than 30 related question on SO and elsewhere, eg 我在SO和其他地方查看了30多个相关问题,例如

Any ideas are appreciated. 任何想法表示赞赏。

Instead of 代替

$("#connection").on("change", connectionSelected());

try 尝试

$("#connection").on("change", connectionSelected);

Note that in the second one I'm passing your function handler by reference, instead of invoking it. 请注意,在第二篇文章中,我通过引用传递了函数处理程序,而不是调用它。

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

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