行上的jQuery验证 [英] jQuery Validation on Rows

查看:52
本文介绍了行上的jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单需要在我的页面上验证。表单如下所示:

I have a form that I need to validate on my page. The form looks like this:

我试图找出最好的方法来确保如果一行中的一条信息被填写;然后需要填写整行。

I am trying to figure out the best way to make sure that if one piece of information in the row is filled out ; then the whole row needs to be filled out.

正如您在示例中所看到的,第3行已经填写了成本但没有标题。第4行有标题,但没有成本。我需要在这些类型的事情上抛出错误。

As you can see in the example, row 3 has the costs filled out but no title. Row 4 has the title but no costs. I need to throw errors on these types of things.

有没有一种简单的方法可以确保是否存在一个数据(在3个可编辑字段中的任意一个),然后整行需要数据。

Is there an easy way to accomplish this to make sure if one piece of data is there (in any of the 3 editable fields) then the whole row needs data.

感谢任何输入

推荐答案

可能的解决方案:

$("#mytable tr").each(function() {
    var anySet = false, allSet = true;
    $(this).find("td input").each(function() {
        var somethingInCell = $(this).val().trim() !== '';
        anySet |= somethingInCell;
        allSet &= somethingInCell;
    });
    if (anySet && !allSet) {
        // there is missing information
    }
});

更新

    $("#mytable").on("input", "input", function(){
        var anySet = false,
            $cells = $(this).closest("tr").find("td input");
        $cells.each(function() {
            var somethingInCell = $(this).val().trim() !== '';
            anySet |= somethingInCell;
        });
        $cells.removeClass("has-error");
        if (anySet) {
            $cells.each(function() {
                if ($(this).val().trim() === '') {
                    $(this).addClass("has-error");
                }
            });
        }
    });

这篇关于行上的jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆