【问题标题】:Triggering a keyup event with jQuery使用 jQuery 触发 keyup 事件
【发布时间】:2015-09-03 18:33:56
【问题描述】:

因此,我正在构建一个“查找您附近的安装程序”功能,该功能允许用户在第 1 页上填写带有邮政编码的输入字段。然后,当他们单击搜索时,它会将他们重定向到 Page2,查找安装程序页面,并使用邮政编码填写搜索框。我已经完成了所有使用邮政编码的工作,并通过 url 将数据传输到新页面,但是一旦邮政编码设置为搜索框中的值,我无法成功触发 .keyup 事件. keyup 将导致结果刷新并使用户高兴地看到他们的结果。

这是我到目前为止的代码,任何帮助/建议将不胜感激!

编辑:作为参考,我使用的是 Flippercode 开发的 Wordpress Google Maps Pro 插件。

// Grab the value when the user clicks the search button,
// and encode it in the url.
$(function () {
    $("#zip-search").bind("click", function () {
        var url = "../find-an-installer?zipField=" + encodeURIComponent($("#zip-field").val());
        window.location.href = url;
    });
});

// Decode the url and grab the value.
var queryString = new Array();
$(function () {
    if (queryString.length == 0) {
        if (window.location.search.split('?').length > 1) {
            var params = window.location.search.split('?')[1].split('&');
            for (var i = 0; i < params.length; i++) {
                var key = params[i].split('=')[0];
                var value = decodeURIComponent(params[i].split('=')[1]);
                queryString[key] = value;
            }
        }
    }
    // Make sure the value is present, and set variables.
    if (queryString["zipField"] != null) {
        var data = "";
        data += queryString["zipField"];
        var userZip = data;

    }

    // Place the value in the search box
    $(function fillValue() {
        $(".wpgmp_search_input").val(userZip);
        console.log("Fill value ran succesfully");
    });

    // Need to figure out how to trigger a keyup once the value is filled,
    // so the search function fires and updates the results.


});

【问题讨论】:

  • textbox 中设置值后只需调用$("#zip-search").click();

标签: javascript jquery wordpress events plugins


【解决方案1】:

从字面上看,当我发布我的问题时,我终于弄明白了。虽然这可能不是最符合语义的,但我绝对愿意改进这个解决方案。

// Grab the value when the user clicks the search button,
// and encode it in the url.
$(function () {
    $("#zip-search").bind("click", function () {
        var url = "../find-an-installer?zipField=" + encodeURIComponent($("#zip-field").val());
        window.location.href = url;
    });
});

// Decode the url and grab the value.
var queryString = new Array();
$(function () {
    if (queryString.length == 0) {
        if (window.location.search.split('?').length > 1) {
            var params = window.location.search.split('?')[1].split('&');
            for (var i = 0; i < params.length; i++) {
                var key = params[i].split('=')[0];
                var value = decodeURIComponent(params[i].split('=')[1]);
                queryString[key] = value;
            }
        }
    }
    // Make sure the value is present, and set variables.
    if (queryString["zipField"] != null) {
        var data = "";
        data += queryString["zipField"];
        var userZip = data;

    }

    // Place the value in the search box
    $(function fillValue() {
        $(".wpgmp_search_input").val(userZip);
        console.log("Fill value ran succesfully");
        // Added this line to fire the keyup
        $('.wpgmp_search_input').keyup();
    });
});

【讨论】:

  • 我在您的代码中没有看到keyUp 事件?你的keyUp 活动到底要做什么?
  • 我在fillValue函数的最后一行添加了——$('.wpgmp_search_input').keyup();因此,在我的情况下,它所做的是导致根据新的 search_input 值刷新结果列表。这是我能找到的最简单的方法,无需修改核心插件文件即可在外部完成。
【解决方案2】:
// Grab the value when the user clicks the search button,
// and encode it in the url.
$(function () {
    $("#zip-search").bind("click", function () {
        var url = "../find-an-installer?zipField=" + encodeURIComponent($("#zip-field").val());
        window.location.href = url;
    });
});

// Decode the url and grab the value.
var queryString = new Array();
$(function () {
    if (queryString.length == 0) {
        if (window.location.search.split('?').length > 1) {
            var params = window.location.search.split('?')[1].split('&');
            for (var i = 0; i < params.length; i++) {
                var key = params[i].split('=')[0];
                var value = decodeURIComponent(params[i].split('=')[1]);
                queryString[key] = value;
            }
        }
    }

    // Make sure the value is present, and set variables.
    if (queryString["zipField"] != null) {
        var data = "";
        data += queryString["zipField"];
        var userZip = data;

    }

    // Place the value in the search box
    $(function fillValue() {
        //if a regular event
        $(".wpgmp_search_input").val(userZip).keyup();
        //if a custom function you've bind
        $(".wpgmp_search_input").val(userZip).trigger("myCustomEvent:keyup");
        console.log("Fill value ran succesfully");
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2011-03-23
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多