简体   繁体   English

使用.each()方法验证jquery中的表单字段

[英]Using .each() method to validate form fields in jquery

I'm trying to build a function using the .each() method in jQuery that scans for empty input fields, highlights them in red, and then provides an alert message to let the user know what needs to change. 我正在尝试使用jQuery中的.each()方法构建一个函数,该函数扫描空的输入字段,将其突出显示为红色,然后提供警告消息以使用户知道需要更改的内容。

Here's my sample html: 这是我的示例html:

<tr>
    <td><input type="number" class="amount required" name="amount" ></td>
    <td><input type="number" class="carrier required" name="carrier" ></td>
    <td><input type="number" class="kilo required" name="kilo" ></td>
</tr>

<button type="submit" class="analyze">Analyze</button>

Here's the function to loop through the table data and add the CSS: 这是循环遍历表数据并添加CSS的函数:

$(".analyze").click(function() {
$(".required").each(function() {
    if ($(this).val() === "") {
        $(this).parents("td").addClass("redClass");
        alert("Looks like some of the fields aren't filled out correctly.  They're highlighted in red.");
    }
});
});

The issue is that the code goes through the array one-by-one and creates an alert message for every single empty cell. 问题在于,代码会一一遍历数组,并为每个空单元格创建一条警报消息。 Ideally it'd add .redClass to the empty fields all at once and just present one alert message at the end if any are empty. 理想情况下,应将.redClass一次性全部添加到空字段中,如果有空则在末尾仅显示一条警报消息。

Per my comment: 根据我的评论:

$(".analyze").click(function() {
    var counter = 0;
    $(".required").each(function() {
        if ($(this).val() === "") {
            $(this).parents("td").addClass("redClass");
            counter++;
        }
    });
    if(counter > 0){
        alert("Looks like some of the fields aren't filled out correctly. They're highlighted in red.");
    }
});

Try this: 尝试这个:

 $(".analyze").click(function () { var req = $(".required"); req.each(function (i) { if ($(this).val() === "") { $(this).parent("td").addClass("redClass"); req.error = true; } }); if (req.error) { alert("Looks like some of the fields aren't filled out correctly. They're highlighted in red."); } }); 
 .redClass { background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <input type="number" class="amount required" name="amount"> </td> <td> <input type="number" class="carrier required" name="carrier"> </td> <td> <input type="number" class="kilo required" name="kilo"> </td> </tr> </table> <button type="submit" class="analyze">Analyze</button> 

Here's a fiddle http://jsfiddle.net/cga65pLh/1/ 这是一个小提琴http://jsfiddle.net/cga65pLh/1/

$(".analyze").click(function() { 
//on click of element with class "analyze"

$(".required").removeClass("redClass"); //remove redClass from all elements
//remove redClass from all elements with class required

x=true;
//turn x to true so that we know a pop up hasn't occurred yet

$(".required").each(function() {
// loop through each element with the class required

if ($(this).val() === "") {
// if the value is empty

    $(this).addClass("redClass");
    //add redClass to the element, turning the background of the element red.

    (x) ? 
 //check if x is true (this is a ternary statement)

 // if x is true do the following:
(alert("Looks like some of the fields aren't filled out correctly. They're highlighted in red."), 
x=false) 
:
// if x is false simply return false and don't do anything 
false;
}

});

});

What this does is, on click, it removes redClass from any element with class "required" this is so that it starts fresh each time the button is pressed. 这样做是在单击时,将其从具有“必需”类的任何元素中删除redClass,以便每次按下按钮时都重新启动。 It then turns the value x to true. 然后将值x变为true。 This way we can keep track of if there's an alert message or not. 这样,我们可以跟踪是否有警报消息。 ae if it's true there's been no alert. AE如果是真的,则没有警报。

For each element with class "required" we check if value is "". 对于具有“必需”类的每个元素,我们检查值是否为“”。 If it is, we apply the class "redClass" which turns the current box red and we check if x is true. 如果是,则应用“ redClass”类将当前框变为红色,并检查x是否为true。 If x is true we display an alert and turn x to false so that no other pop ups occur. 如果x为true,则显示警报,并将x设置为false,这样就不会出现其他弹出窗口。

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

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