我使用keycode
觸發按鈕單擊輸入。我有以下內容,當警報被放置在鍵碼功能中時正在工作,但當放置在.click(function...
中時沒有。不知道我錯過了什麼讓keycode觸發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,觸發器正常工作。隨着包括的線,它似乎被阻止。
檢查控制檯,修正錯誤。那麼它能解決你的問題嗎? –
如果我在控制檯中有任何錯誤,它可能......但在我的代碼中沒有問題。 – Matt