How to submit form through jQuery onblur

折月煮酒 提交于 2021-02-09 10:26:46

问题


So I am trying to submit the form through jQuery onblur (i.e. As soon as the focus leaves the password field, form submits through jQuery). There are similar questions but it's not what I am looking for. I tried using the document.getElementById but it isn't working.

Any help is appreciated. Thanks in advance.

<script>
    $(function(){
       $("#pswd").blur({
          $("#myForm").submit(); 
       });

    });

</script>

回答1:


As @dvenkatsagar suggested, you have some syntax errors,

Live Preview: http://codepen.io/larryjoelane/pen/VedXqr

(function($){

       //I selected the body tag first because I do not know if you are creating
       //the pswd element dynamically
       $("body").on("blur","#pswd",function(e){


          $("#myForm").submit(); 


       });

      })(jQuery);

It is not necessary but I changed the way your closure was structured just in case you want to use other libraries in the future inside your closure besides jQuery.




回答2:


Well the only problem I can see the function blur() and the way it is written, I think you should change it like this:

$(function(){
   $("#pswd").on("blur",function(){
      $("#myForm").submit(); 
   });

})();


来源:https://stackoverflow.com/questions/35109582/how-to-submit-form-through-jquery-onblur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!