The following code is used so when clicking 'enter' when focused in an input field within a form, it disables the standard submit button functionality. Rather the javascript builds a new form and sends a post request to another page. I am using this so that I can achieve a "form within a form" functionality. The input field has its own submit button for which I have similar code to perform the post.
In firefox, this works perfectly; in IE hitting enter does nothing at all. Here is the code:
//event handler for input field within standard form
$('#inputfield').bind('keypress', function(e) {
var code = e.keyCode || e.which;
if(code==13){
//suppress default submit functionality
$('form').submit(function () {
return false;
});
//...assemble some data here...
var url_string = "...";
var $form = $("<form method='POST'>").attr("action", url_string);
$("<input type='hidden' name='body'>").attr("value",body).appendTo($form);
$("<input type='hidden' name='title'>").attr("value",title).appendTo($form);
//...create more form elements here...
$form.appendTo("body");
$form.submit();
}
});
Any ideas why this isn't working in IE (I tried in IE 6,7 and 8)?
Update: just to elaborate on the problem: in firefox, the $form.submit();
line at the end causes the jquery generated form to submit and the page to redirect. In IE nothing happens at all. The keypress does fire in IE, its just that the form doesnt seem to get submitted.