最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jquery, submit form when field loses focus - Stack Overflow

matteradmin18PV0评论

How can I submit a form when a field (In this case the form only has one field) loses focus?

I tried this, but it's not working:

$("form").submit();

UPDATE

I forgot to mention that the form was created with jquery as well:

$("div").html('<form action="javascript:void(0)" style="display:inline;"><input type="text" value="' + oldValue + '"></form>');

This is probably why it won't submit, I think it's because the events aren't being observed.

How can I submit a form when a field (In this case the form only has one field) loses focus?

I tried this, but it's not working:

$("form").submit();

UPDATE

I forgot to mention that the form was created with jquery as well:

$("div").html('<form action="javascript:void(0)" style="display:inline;"><input type="text" value="' + oldValue + '"></form>');

This is probably why it won't submit, I think it's because the events aren't being observed.

Share Improve this question edited Dec 6, 2009 at 23:44 JP Silvashy asked Dec 6, 2009 at 23:35 JP SilvashyJP Silvashy 48.5k50 gold badges153 silver badges230 bronze badges 2
  • What do you hope to achieve with action="javascript:void(0)"? – Crescent Fresh Commented Dec 7, 2009 at 0:17
  • meh, I don;t know something crappy the rails plugin i'm using decides to put in there. – JP Silvashy Commented Dec 7, 2009 at 1:15
Add a comment  | 

3 Answers 3

Reset to default 10
$('form :input').blur(function() {
    $(this).closest('form').submit();
});

Trigger the form's submit() event when the field loses focus. You can detect this by attaching a blur() event handler to it.

$("#field").blur(function() {
  $("#form").submit();
});

If you field doesn't have an ID or other means of easily identifying it (which I would recommend) you could also do something like this:

$("#form :input").blur(function() {
  $("#form").submit();
});

since there's only one field.

What about this

var $yourForm = $('#form');

$yourForm.find('input').eq(0).blur(function() {

    $yourForm.submit();
});

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far