Jquery, send form when field loses focus

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

I tried this, but it does not work:

$("form").submit();

UPDATE

I forgot to mention that the form was created using jquery:

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

This is probably why he does not obey, I think, because events are not observed.

+3
source share
3 answers

Run the submit () form when the field loses focus. You can detect this by adding blur()an event handler to it .

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

If you don’t have an identifier or other means to simply define it (which I would recommend), you can also do something like this:

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

.

+4
$('form :input').blur(function() {
    $(this).closest('form').submit();
});
+7

How about this

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

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

    $yourForm.submit();
});
+1
source

Source: https://habr.com/ru/post/1724891/


All Articles