1

llo, I have a form that has two submit buttons: one accept and one cancel:

<p>
 <input type="submit" name="accio" value="accept" class="default" />
 <input type="submit" name="accio" value="cancel" class="cancel" />
</p>

I want to submit the form with jQuery using the keyboard:

$(document).ready(function() {
 $("input[type=text]").focus();
 $("input[type=text]").keydown(function(e) {
 if ((e.keycode && e.keycode == 13) || (e.which && e.which == 13)) {
 $(".default").submit();
 }
 if ((e.keycode && e.keycode == 27) || (e.which && e.which == 27)) {
 $(".cancel").submit();
 }
 });
});

and I want the parameter accio to contain accept or cancel depending on the key pressed (I process it in the server)... It works in Firefox, but not in IE nor in Chrome. Where's my fault?

Update

Following @Sime's I got the following code:

$(document).ready(function() {
 $("input:text").focus();
 $("input:text").keydown(function(e) {
 if (e.which == 13) {
 $("#default").submit();
 }
 if (e.which == 27) {
 $("#cancel").submit();
 }
 });
});

but it's still not working... I think it's with the way I submit the form... Can't I call the submit method on a "input:submit" and expect that the correct values are passed on to the controller?

asked Mar 8, 2011 at 16:48
6
  • @Charles Better: input:text Commented Mar 8, 2011 at 16:54
  • @Charles Also, you don't have to do these tests on the event object. jQuery makes sure that e.which is available. Commented Mar 8, 2011 at 16:56
  • @Charles Also, are you sure that you want to submit a whole class of objects? Commented Mar 8, 2011 at 16:58
  • I thought id made funny things with the submitted value... I'll try and change that and remove my tests on e.which... Thanks. Commented Mar 8, 2011 at 17:02
  • @Carles The submit() method works on FORM elements. You are trying to call it on the buttons. Commented Mar 8, 2011 at 17:21

2 Answers 2

3

try this:

$("input[type=text]").keydown(function(e) {
 if ((e.keycode && e.keycode == 13) || (e.which && e.which == 13)) {
 $(".default")[0].click();
 }
 if ((e.keycode && e.keycode == 27) || (e.which && e.which == 27)) {
 $(".cancel")[0].click();
 }
});

could you put a way to better identify the buttons.

$("p input.cancel")

UPDATE:

$(document).ready(function() {
 $("input:text").focus();
 $("input:text").keydown(function(e) {
 if (e.which == 13) {
 $("#default")[0].click();
 }
 if (e.which == 27) {
 $("#cancel")[0].click();
 }
 });
});

see this example

UPDATE II

this code functiona well in IE6 +, the correction is in the stopPropagation

$('input[type="text"]').keydown(function(ev) {
 if (ev.which == 13 || ev.which == 27) {
 ev.stopPropagation();
 if (ev.which == 13)
 $("#default")[0].click();
 if (ev.which == 27)
 $("#cancel")[0].click();
 return false
 }
});
answered Mar 8, 2011 at 17:11
Sign up to request clarification or add additional context in comments.

5 Comments

Still not working... I get null for the accio parameter in the first case (enter pressed) and the form clears it's values, but nothing is submitted in the second case (escape key). Thanks.
This should work. As mentioned above, you can just use e.which (jQuery makes sure it's there). JSFiddle: jsfiddle.net/Uv79u/1
With jQuery you should use "which", as it takes to support every browser in this way if (event.which == null & & (event.charCode! = null | | event.keyCode! = null)) { event.which = event.charCode! = null? event.charCode: event.keyCode; }
It works in Firefox and Chrome. It doesn't work in IE8... Weird.
It works with the last update. I suppose stop propagation prevents the event to continue it's normal execution. Thanks!
0

Try the following:

$( '#myForm' ).submit();
$( '#mySubmitButton' ).click();
answered Mar 8, 2011 at 16:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.