我有一个ajax调用,它从请求返回到服务器,需要动态地将任何OnKey事件添加到输入元素,这里是我正在使用的代码,并注意我已尝试使用的注释掉的脚本但是没工作。这项工作在IE中没有问题,但我使用的是Mozillas最新的浏览器。
function HomePageSearchKeyPressEnter(e,searchType,val) {
alert('eeeee');
var e = e || window.event;
if (e) {
alert('sssss');
var code = e.keyCode || e.which;
if (code == "13") {
HomePageSearchEnter(searchType, val);
}
}
}
function HomePageSearchEnter(searchType, val) {
if (searchType == 'Music') {
WebForm_DoCallback("UserPageControl1", "HomePageSearchText~" + searchType + '~' + val, UserHomePageNavigationClickServerResponse, null, null, true)
}
}
function UserPageHomeSearchServerResponse(param, context) {
if(param.length > 0) {
var splitParam = param.split("|");
var html = splitParam[0];
var id = splitParam[1];
var searchType = splitParam[2];
$("#" + id).html(html);
var elem = document.getElementById('HomePageSearchTextboxID');
alert('ddd');
if (elem.addEventListener) {
elem.addEventListener("keydown", function() { HomePageSearchKeyPressEnter(event, searchType, elem.value); }, false);
}
else {
elem.attachEvent("onkeypress", function() { HomePageSearchKeyPressEnter(event, searchType, elem.value); });
}
// elem.onkeyup = function() { HomePageSearchKeyPressEnter(event, searchType, this.value); };
alert('ddssssd');
// $("#HomePageSearchTextboxID").attr("onkeyup", function() { HomePageSearchKeyPressEnter(event, searchType, this.value); });
}
}
答案 0 :(得分:0)
尝试使用JQuery:
$('#target').keydown(function() { alert('Handler for .keydown() called.'); });
我在我的网站上使用了类似的东西,它适用于chrome,ff,&即
此处有更多信息:http://api.jquery.com/keydown/