开发者

Onkey events is not working when dynamically adding to an element in Mozilla

开发者 https://www.devze.com 2023-02-18 23:07 出处:网络
I have an ajax call that come back from a request to the server and needs to dynamically add the any OnKey events to an input element, here is the code I\'m using and note the commented out script I h

I have an ajax call that come back from a request to the server and needs to dynamically add the any OnKey events to an input element, here is the code I'm using and note the commented out script I have also tried to use but didn't work. This works in IE with no problem, but I'm using Mozilla's newest browser.

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_D开发者_StackOverflowoCallback("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); });
}
}


Try using JQuery:

$('#target').keydown(function() { alert('Handler for .keydown() called.'); });

I used something similar to this for my site and it works in chrome,ff, & ie

more info here: http://api.jquery.com/keydown/

0

精彩评论

暂无评论...
验证码 换一张
取 消