I created a search field (id:searchField) and a search button (id:searchButton) using Xpages Custom Controls. I added an onkeypress event on the search field such that it will trigger a click to the searchButton. The searchButton will then reload the page but with url parameters coming from the search field. The problem is that the page reloads but the search parameters are not added to the URL when I press ENTER in the search field, but works properly when I press searchButton. Here are the codes I used:
(code added to the onkeypress of searchField)
if (typeof thisEvent == 'undefined' && window.event) { thisEvent = window.event; } if (thisEvent.keyCode == 13) { document.getElementById("#{id:searchButton}").click(); }
(code added to the onclick of searchButton)
window.location.href = "test.xsp?search=" + document.getElementById("#{id:searchField}").value;
I tested it in IE and Firefox, both have problems. I created a sample HTML file and it worked correctly. Is this a bug of XPages or am I missing something here?
Add this after your '.click()':
thisEvent.preventDefault();
thisEvent.stopPropagation();
It should solve the problem ;-)
Changing the onKeyPress event of the input field to
if (typeof thisEvent == 'undefined' && window.event) { thisEvent = window.event; }
if (thisEvent.keyCode == dojo.keys.ENTER)
{
dojo.byId("#{id:searchButton}").click();
thisEvent.preventDefault();
}
should be sufficient to solve the problem. Note that for cross browser compatibility I've used the
dojo.keys.ENTER
and
dojo.byId("id");
property/ method. The dojo.keys object has a lot more properties to check for certain key presses: see here
Mark
I've done this just recently in an XPage, and the following script works for me cross-browser:
var e = arguments[0] || window.event;
if ( e.keyCode==13 || e.which==13) {
window.location.href = 'searchResults.xsp?query=' +
encodeURI(dojo.byId('#{id:searchInput}').value));
return false;
}
Hope this helps,
Jeremy
Issue is there with the id, generated by xpage. I had a same issue. xPages prefix the id of custom control like view:_id1:_id... Try by giving complete id
来源:https://stackoverflow.com/questions/9145536/xpages-onkeypress-event-not-triggering-click-properly