I am working on a problem to associate a “enter” button with submit action with JQuery. It is probably a relatively easy work for JQuery expert, but it did took me some time to find a good solution.
- Include the required javascript file. The way I used to include javascript files is to register them in Page_Init of code-behind.
protected void Page_Init(object sender, EventArgs e)
{
HtmlGenericControl jQueryScript = new HtmlGenericControl("script");
jQueryScript.Attributes.Add("type", "text/javascript");
jQueryScript.Attributes.Add("src", ResolveClientUrl("~/Includes/jquery-1.2.6.min.js"));
this.Page.Header.Controls.Add(jQueryScript);
}
- Register the keydown event with the document, and trigger the submit action when the keydown code is 13. I identify the default submit button with a special css attribute “defaultbutton”, since the asp.net web form framework create complicated Ids and names, it’s much easier to use the css class instead.
$(document).keydown(function(event) {
if (event.keyCode == 13) {
//not working: __doPostBack('__Page', 'EnteryKey');
//find default button.
var submitButton = $(".defaultbutton").get(0);
if (submitButton != null) {
submitButton.click();
}
}
});
That’s it.
0 comments:
Post a Comment