2

So, the page has an action which must be invoked when "enter" is pressed.

VISUALFORCE:

//script:
function pressReturn(e, currOffer) {
    var keyPressed = e.keyCode;
    if (keyPressed == 13) {    
        insertOffer(currOffer);        
    }
}

//Action Function:
<apex:actionFunction name="insertOffer" action="{!setNewBidOffer}">
     <apex:param name="currOffer" value="currOffer" assignTo="{!currentOffer}"/>
</apex:actionFunction>

//input fiel:
<apex:inputText value="{!currentOffer}" onkeypress="return pressReturn(event, j$(this).val());"/>

The action invoked by the controller (setNewBidOffer) does stuff and then returns a PageReference null.

In chrome everything works fine: the user writes, presses enter, the javascript is called, setNewBidOffer is done, null is returned and the page is refreshed.

This is not happening in mozilla and in IE. I tried to set some debug logs in the action setNewBidOffer and apparently, when I use mozilla or IE, the action in the controller is not even called.

What am I missing? thank you very much for your patience and attention!

2 Answers 2

3

Try to use jQuery to trigger on the keyPress/keyDown events. Additionally i leave the value parameter empty if i want to set it from another javascript function and use reRender to refresh some areas:

<script>
jQuery('[id$=myField]').on('keydown keypress keyup', function(event) {
    if (event.keyCode == 13) {
        event.preventDefault();
        insertOffer(jQuery(this).val());
    }
});
</script>


<apex:actionFunction name="insertOffer" action="{!setNewBidOffer}" reRender="myField">
     <apex:param name="currOffer" value="" assignTo="{!currentOffer}"/>
</apex:actionFunction>

<apex:inputText value="{!currentOffer}" id="myField"/>

But one thing i don't understand: you trying to set the currentOffer variable to the currentOffer variable?

1
  • event.preventDefault(); <--- this made the magic! Thank you very much!
    – T. Rossi
    Commented Jun 4, 2013 at 13:42
0

Event.preventDefualt() made the magic and also want to highlight one point;
The possible reasons in which action function fails to work:
1) if in case any other JS function fails
2) in my case, I was setting date from JS to apex class(via apex: param of action function) and storing this value as a Date in apex class,
I changed it to string as all JS parameters are either String or Number, it works... :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .