views:

428

answers:

2

Hi,
I use rich:inputNumberSpinner tag.

The problem is :

I set cursor focus to inside of rich:inputNumberSpinner field, then i hit the enter button from my keyboard, that time page will be automatically refresh.

But i don't need page refresh when i hit the enter button from my keyboard.

The code :

spinnerTagTest.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j" %>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;

<f:view>
    <html>
      <head>
       <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">              
     </head>
<body>
      <h:form id="SpinnerForm">                                  
              <rich:inputNumberSpinner id="spinnerField" value="" />              
      </h:form>
</body> </html></f:view>

I also use rich:hotKey for that inputNumberSpinner field. But Page refreshed.

<rich:hotKey key="return"
             selector="#spinnerField"
             handler="event.stopPropagation();event.preventDefault();
             return false;"/>

And i check anotherway using javasccript, but page refreshed.

The specific tag and javascript is :

<rich:inputNumberSpinner id="spinnerField"  value="" 
                         oninputkeypress="return stopPageRefresh();"/>

<script type="text/javascript">

    function stopPageRefresh()
    {
        return false;
    }
</script>

Here i use one alert message inside of stopPageRefresh().
But i hit enter button, first page refreshing and then alert message displayed.

Help me about this. Thanks in advance.

A: 

I'm not a JSF expert, but I think the problem is only related to client-side JavaScript.

In several browsers you cannot prevent default actions using onkeypress (in some browsers you can, in some browsers this is the only way of preventing default actions). Anyway, this is a great mess with a lot of differences between browsers.

Following code will add the appropriate event handler to spinnerField. Run it inside an onload event handler, or at least after spinnerField is rendered.

if (window.opera)
  document.getElementById("spinnerField").onkeypress = noEnter;
else
  document.getElementById("spinnerField").onkeydown = noEnter;

function noEnter(e) {
  var event = e || window.event;

  if (event.keyCode == 13)
    return false;
}

Update: Possibly you have to set the cancelBubble property in IE to fully prevent submitting on Enter, but I'm not sure about that. If the above example doesn't work properly in Microsoft's product, replace noEnter with:

function noEnter(e) {
  var event = e || window.event;

  if (event.keyCode == 13) {
    event.cancelBubble = true;
    return false;
  }
}
Marcel Korpel
Thanks for your effort. But i test one alert message inside of your script. That alert also called after refreshing the page.
EswaraMoorthyNEC
@eswa: Alert message? Where do you put that line?
Marcel Korpel
After open the braces, i add the alert message.funtion noEnter(e){ alert("start process..."); //Here i use your code }
EswaraMoorthyNEC
@eswa: In that case, the alert will pop up on every key press. E.g., if you place it inside the `if` block, it will only fire when you press Enter.
Marcel Korpel
A: 

Hi Marcel Korpel

Just i modify your script like add one alert message.

<script type="text/javascript">

document.onkeydown=noEnter;
if (window.opera)
      document.getElementById("spinnerField").onkeypress = noEnter;
else
     document.getElementById("spinnerField").onkeydown = noEnter;

function noEnter(e)
{
      alert("start...");
      var event = e || window.event;
      if (event.keyCode == 13)
      {
         event.preventDefault();
         return false;
      }
}
EswaraMoorthyNEC
Here before open the popup (alert), page refreshed.
EswaraMoorthyNEC
I feel this is JSF component related problem.If you know jsf related forum, then please can you mentioned.Help me.
EswaraMoorthyNEC
You shouldn't attach an `onkeydown` event in every case… Moreover, this way the alert will pop up on every key press. If you place it inside the if block, it will only fire when you press Enter.
Marcel Korpel
Do you have an example page, so we can look at the actually generated HTML?
Marcel Korpel
ok. That case also i check it. Like, i insert alert message in inside of if block. when i hit enter button from that rich:inputNumberSpinner field, then first page refreshed, and then alert winwow open.
EswaraMoorthyNEC
In which browser? And still, do you have an example page, so we can check this?
Marcel Korpel