如何在Jquery / Javascript中编写keypress事件?

时间:2011-08-01 11:11:29

标签: javascript jquery

我是Jquery和Javascript的新手。需要有关此问题的帮助:

有一个名为Profile的div,如果我点击它,则会打开一个新的弹出窗口。现在只有通过鼠标点击才能实现这一点。我需要通过按键(输入)来完成。

DIV的JSP代码

<div tabindex="0" style="text-decoration:underline" onmouseover="this.style.cursor='pointer';" id="<%=namespace%>_pmProfileName_<%=i%>_" onclick="perfChartConfig_<%=namespace%>.doShowProfilesForElement(this);">Profile</div>

JQUERY / JAVASCRIPT CODE

    doShowProfilesForElement : function(source){

    var callback = {       
     success : function(r) { 
        MO.globals.popups.hideWorkingMsg();  
        this.argument[0].pe = eval("("+r.responseText+")");  
        var pe = this.argument[0].pe; 

        var pStr = '';
        if(this.argument[1].indexOf("pmProfileName") > 0){
            if(pe == null || pe._mosol_AllPerfProfileNames.length==0){
                pStr = pStr + "<li>There are no profiles available for the element you selected.</li>";
            } else {
                for(var i=0; i<pe._mosol_AllPerfProfileNames.length;i++){
                    var profile = pe._mosol_AllPerfProfileNames[i];
                    var onClick = "onClick=\"perfChartConfig_<%=namespace%>.doProfileSelection(this,'"+this.argument[1]+"');\"";
                    pStr = pStr + "<div style=\"text-decoration:underline\" onmouseover=\"this.style.cursor='pointer';\" "+onClick+" >"+profile+"</div>";
                    //alert('For profile ['+profile+'] there are '+pe[profile].length+' expressions.');         
                }
            }
        }
        if(this.argument[1].indexOf("slaProfileName") > 0){
            if(pe == null || pe.offers.length==0){
                pStr = pStr + "<li>There are no SLAs available for the element you selected.</li>";
            } else {
                for(var i=0; i<pe.offers.length;i++){
                    var profile = pe.offers[i];
                    var onClick = "onClick=\"perfChartConfig_<%=namespace%>.doProfileSelection(this,'"+this.argument[1]+"');\"";
                    pStr = pStr + "<div style=\"text-decoration:underline\" onmouseover=\"this.style.cursor='pointer';\" "+onClick+" >"+profile+"</div>";
                }
            }
        }


        var rp = perfChartConfig_<%=namespace%>.rp;
        rp.setHeader("Select a Profile you want to chart:"); 
        rp.setBody(pStr); 
        rp.render();     
        rp.show();  
     },         
     failure : function(r) { 
        MO.globals.popups.hideWorkingMsg();
        MO.globals.popups.showMsg("Error", "<h2><span class=\"portlet-msg-error\">Your submission failed</span>"+"<br />Status: " + r.status + "</h2>");
     },    
     argument: [this, source.id]    
   };

你能告诉我如何编写按键功能(输入)吗?

2 个答案:

答案 0 :(得分:1)

bind {div}的keypresskeyup事件

<div tabindex="0" class="someClass" style="text-decoration:underline" onmouseover="this.style.cursor='pointer';" id="<%=namespace%>_pmProfileName_<%=i%>_" onclick="perfChartConfig_<%=namespace%>.doShowProfilesForElement(this);">Profile</div>

添加一些课程

$("div.someClass").bind("keyup",function(e){

if(e.keyCode == 13)
{
$(this).click();
}

});

答案 1 :(得分:1)

$("#Profile").keyup(function(e){ 
var code = e.which; 
if(code==13){
e.preventDefault();

    //Do Stuff
}
});