2
function onLoad() {
    eworkData.FieldByName('SearchReference').HTMLfield.onkeydown=function(evt)
    {
        var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
        if( keyCode == 13 )
        {
            eworkData.FieldByName('cmdSearch').HTMLfield.onfocus();
            eworkData.FieldByName('cmdSearch').HTMLfield.onclick();
        }
    };
    eworkData.FieldByName('SearchFirstName').HTMLfield.onkeydown=function(evt)
    {
        var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
        if( keyCode == 13 )
        {
            eworkData.FieldByName('cmdSearch').HTMLfield.onfocus();
            eworkData.FieldByName('cmdSearch').HTMLfield.onclick();
        }
    };
    // more controls
}

有没有更整洁的方法来做到这一点?更小的紧凑代码?我认为在 jQuery 中我可以做到

$("SearchReference, SearchFirstName").onkeydown(function() ....)

但是,jQuery 不是我的选择:o(

4

2 回答 2

1
var theElement = document.getElementById("YOUR_ELEMENT");
theElement.addEventListener("keypress", function(e) {
    switch(e.keyCode) {
        case "13":
           //keycode 13 pressed...
           break;
    }
});

或 jQuery:

$("YOUR_ELEMENT").live("keypress", function(e) {
   switch(e.keyCode) {

   }
});
于 2012-08-09T08:28:48.823 回答
1

尝试:

function onLoad() {
    var fields = ["SearchReference", "SearchFirstName"];
    for(var i = 0; i < fields.length; i++) {
        eworkData.FieldByName( fields[i] ).HTMLfield.onkeydown=function(evt) {
            var keyCode = evt.keyCode;
            if( keyCode == 13 ) {
                eworkData.FieldByName('cmdSearch').HTMLfield.onfocus();
                eworkData.FieldByName('cmdSearch').HTMLfield.onclick();
            }
        };
    }
}
于 2012-08-09T08:30:15.627 回答