检测是否在jQuery中填充了至少一个输入(作为数组) [英] Detecting if at least one input (as array) is filled in jQuery

查看:71
本文介绍了检测是否在jQuery中填充了至少一个输入(作为数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组输入,例如<input type="text" name="clients[]" />,它们是通过AJAX加载的.在用户提交表单之前,我需要检查其中至少一项是否已填写.如果是数组,我不知道该怎么做.

I've a group of inputs like <input type="text" name="clients[]" /> that are loaded via AJAX. Before the user submits the form, I need to check if at least one of them has been filled. I don't know how to do this if is an array.

提前谢谢!

推荐答案

将函数绑定到表单的Submit事件中:

Bind a function to the submit event for the form like this:

// assuming #foo is the form and each grouped element has classname bar
// also, this assumes that your form is not loaded by AJAX, but only the elements
// contained in it
$('#foo').submit(function() {
    var elements = $("input[name='clients[]']");
    var validated = false;
    elements.each(function() {
        if ($(this).val() != '') {
            validated = true;
            return false; // will break the each
        } else {
            validated = false;
        }
    });
    return validated;
});

此处的示例.

这篇关于检测是否在jQuery中填充了至少一个输入(作为数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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