开发者

jquery, submit form when field loses focus

开发者 https://www.devze.com 2022-12-13 14:10 出处:网络
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:

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")开发者_JAVA百科.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.


$('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();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号