[英]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.