输入键上的键码触发器无法正常工作

时间:2015-05-11 17:51:33

标签: javascript jquery keycode

我正在使用keycode触发按钮点击输入。我有以下内容,当它们在键码函数中放置警报时工作,但放在.click(function...时则不行。不知道我缺少什么来获取键码来触发ajax调用。它目前只是将?imageid=750推送到URL中查询字符串的末尾。

//force use of only numerical input
$('#imageid').keyup(function (event) {
    this.value = this.value.replace(/[^0-9\.]/g, '');
    if (event.keyCode == 13) {
          alert($("#imageid").val());
        //event.preventDefault();
        $('.image-search').trigger('click');
    }
});



$('.image-search').click( function () {
    var root = "http://localhost:7777/proxy/staging/rest/v1/cms/story/id/";
    //CLEAR IMAGE ID VALUE FROM PREVIOUS ENTRIES
    $("#imageid").val('');
    var encodeID = $("#imageid").val();
    var bearerToken = localStorage.getItem('Authorization');

    //IF TESTING ON LOCALHOST
    if (document.domain == 'localhost') {
        url = root + encodeID + "/images";
    } else {
        //IF IN PRODUCTION
        url = "/cropper/admin/cropv2/rest/v1/cms/story/id/" + encodeID + "/images";
        //GRAB REFERRER URL FOR ASSET ID
        //STRIP OUT ASSET ID, MATCH BUT DON'T INCLUDE 'ASSETS' AND SET SUCCESEEDING VALUE AS ASSET ID
        var regexp = /assets\/(\d+)/;
        var assetid = regexp.exec(window.document.referrer);
        $("#imageid").val(assetid[1]);
    };
    $.ajax({
        url: url,
        method: 'GET',
        headers: {
            "Authorization": bearerToken
        },
    }).then(function (response) {
        var obj = response;
        $("#imageid").css("border-color", "#ccc");
        $(".search-results").empty();
        for (var property in obj.entity.entries) {
            if (obj.entity.entries.hasOwnProperty(property)) {
                $(".search-results").append($("<li><a href='" + obj.entity.entries[property].uri + "' target='_blank'><div class='thumbnail'><img width='30' height='30' src='" + obj.entity.entries[property].uri + "' target='_blank'/></img><div class='caption'><p>" + obj.entity.entries[property].orientation + "</p></div></a></li>"));
            }
        }
        //$(".search-results").append("<div class='caption'>" + data.id + "</div><div class='thumbnail'><img width='40' height='40' src='" + data.thumbnailUrl + "'/></img>").css("float", "left");
    }).fail(function (data) {
        $(".search-results").empty();
        $(".search-results").append("<p class='alert alert-danger'>Invalid ID</p>");
        $("#imageid").css("border-color", "red");
    });
});
});

但是当在整个项目中实施时,它不再正常工作。我不确定我在这里缺少什么。

使用此小提琴:JSFIDDLE,并删除第1-173行,触发器正常工作。如果包含这些行,它似乎被阻止了。

2 个答案:

答案 0 :(得分:0)

您的代码运行正常,您正在出现导致脚本停止的其他Javascript错误。

另请尝试使用$('.image-search').click();代替$('.image-search').trigger('click');

答案 1 :(得分:0)

  1. 未捕获的ReferenceError:未定义ImglyKit
  2. 未捕获的TypeError:无法读取属性&#39; addEventListener&#39; of null(fileInput)