Here is how I solved the problem.
First I created AbstractOnloadEventBehavior and events.js. This allows me to
execute multiple scripts (behaviors) in the window.onload callback.
Then I created AjaxBindEnterKeyBehavior which just intercepts the enter key
on a form and clicks the ajax submit button instead.
I tested on IE7 and FF2.0
Review my work and if no one sees any apparent problems I'll post it in the
wiki.
import wicket.ajax.markup.html.form.AjaxSubmitButton;
import wicket.markup.html.form.Form;
public class AjaxBindEnterKeyBehavior extends AbstractOnloadEventBehavior {
private AjaxSubmitButton button;
public AjaxBindEnterKeyBehavior(AjaxSubmitButton button) {
this.button = button;
}
@Override
protected String getScript() {
Form form = (Form) button.findParent(Form.class);
return "document.getElementById('" + form.getMarkupId()
+ "').onkeydown = function (e) {\n"
+ "var evt = (e) ? e : window.event; \n"
+ "var keyCode = evt.keyCode;\n" + "if (keyCode
== 13) {\n "
+ "document.getElementById('" +
button.getMarkupId()
+ "').click();" + "return false; \n } }";
}
}
events.js
-----------------------------------------------
function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, true);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}
----------------------------------------------
import org.apache.commons.lang.RandomStringUtils;
import wicket.RequestCycle;
import wicket.ResourceReference;
import wicket.Response;
import wicket.behavior.AbstractAjaxBehavior;
import wicket.markup.html.IHeaderContributor;
import wicket.markup.html.resources.CompressedResourceReference;
import wicket.util.string.JavascriptUtils;
abstract public class AbstractOnloadEventBehavior extends
AbstractAjaxBehavior
implements IHeaderContributor {
/** reference to the event javascript file. */
private static final ResourceReference JAVASCRIPT = new
CompressedResourceReference(
AbstractOnloadEventBehavior.class, "events.js");
@Override
protected String getImplementationId() {
return "events";
}
@Override
public void onRequest() {
}
@Override
protected void onRenderHeadInitContribution(Response response) {
CharSequence url = RequestCycle.get().urlFor(JAVASCRIPT);
JavascriptUtils.writeJavascriptUrl(response, url);
}
@Override
protected void onRenderHeadContribution(Response response) {
String functionName = RandomStringUtils.randomAlphabetic(8);
response.write("<script type=\"text/javascript\"> function "
+ functionName + "() {" + getScript()
+ "} addEvent(window, 'load', " + functionName
+ ");</script>");
}
protected abstract String getScript();
}
igor.vaynberg wrote:
>
> you have to catch the enter keypress on every textfield and submit the
> form
> using ajax. you can do that using some javascript magic and some behavior
> that adds this magic to onload event.
>
> so in your basepage or baseform youd do: add(new
> FixIe7EnterKeyBehavior());
>
> -igor
>
>
> On 5/16/07, Joe Toth <[EMAIL PROTECTED]> wrote:
>>
>> Using Wicket 1.2.6
>>
>> Using FF 2.0 when I click on the submit button or hit 'enter' on a field
>> in the form the ajax call is processed.
>>
>> With IE 7.0 when I click on the submit button, the ajax class is
>> processed, BUT when I hit 'enter' the form submits normally.
>>
>> Any idea what I can do to keep the same behavior all around and make IE 7
>> 'enter' behave the same as the click?
>>
>> Thanks
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by DB2 Express
>> Download DB2 Express C - the FREE version of DB2 express and take
>> control of your XML. No limits. Just data. Click to get it now.
>> http://sourceforge.net/powerbar/db2/
>> _______________________________________________
>> Wicket-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/wicket-user
>>
>>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Wicket-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/wicket-user
>
>
--
View this message in context:
http://www.nabble.com/AjaxSubmitButton-IE-vs.-FF-Behavior-tf3769244.html#a10675702
Sent from the Wicket - User mailing list archive at Nabble.com.
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user