繁体   English   中英

如何使用jQuery验证每个焦点事件的多个输入?

[英]How to validate multiple inputs every focusout event using jquery?

大家好,我在使用focusout事件验证输入时遇到问题。 我不想要的是,每次对输入进行focusout它只会在不影响其他输入的情况下验证该输入。 问题是我输入很多,并且我不想使用相同的ID。 这就是问题所在,因为当我的输入之一触发时,它会验证所有内容。 我对此进行了很多研究,其中一些与我的问题有关,但我仍然不明白,我还是编程新手。 我需要帮助!

继承人链接: https : //jsfiddle.net/m7wa35tc/

 var Main = { init: function(){ this.handleBinds(); }, handleBinds: function(){ $('.required').on("keyup", function(){ if($.trim($(this).val()).length < 1){ $(this).css("border-color", "red"); }else{ $(this).css("border-color", ""); } }); } }; $(document).ready(function(){ Main.init(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>Name <input type="text" id="required"> </label> <br> <label>Corporate or Legal Name <input type="text" id="required"> </label> <br> <label>Location Address <input type="text" id="required"> </label> <br> <label>City, State <input type="text" id="required"> </label> <br> <label>Zip/Postal Code <input type="text" id="required"> </label> <br> <label>Country <input type="text" id="required"> </label> <br> <label>Email Addresse <input type="text" id="required"> </label> 

您需要为要引用的每个元素设置唯一的ID,在这种情况下,请使用class =“ required”代替

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM