如何检查页面中是否有任何文本框都输入了文本?

时间:2013-08-30 05:40:22

标签: javascript jquery jquery-validate

我正在使用ajax,javascript和jquery在php中开发一个网站。

在一种形式中,根据用户的状态,存在动态数量的文本框。文本框ID为'tb'+ id,如tb1,tb5,tb25,tb60等,所有文本框类都为'po'。

现在点击保存按钮我需要验证这些'tb'文本框。 所有文本框都应该是空白的,或者都不是。

任何人都可以帮我实现这个目标吗?

4 个答案:

答案 0 :(得分:2)

尝试

var $pos = $('.po');
var entered = $po.filter(function(){
    return $.trim(this.value) == '';
});

if(entered.length != 0 && $pos.length != entered.length){
    //invalid since some are left blank
}

答案 1 :(得分:2)

var txt_box_count  = $(".po").length;

var empty_text_boxe_count = $(".po").filter(function(){
    return $.trim($(this).val()) == '';
}).length;


if(empty_text_box_count == 0)
{
     alert("all text fields are filled");   
}
else if(txt_box_count == empty_text_boxe_count)
{
     alert("all text fields are empty");
}

答案 2 :(得分:1)

试试这个:

var isValid = $('.po').filter(function(){ return !$(this).text();});

if(isValid.length){ /* or if(isValid[0]){
    //some textbox is empty
}
else{
    //all are filled
}

答案 3 :(得分:0)

试试这段代码

        var tLength = $('.po').length;
        var allEmpty = $('.po').filter(function () {
            return $.trim($(this).val()).length > 0;
        });
        var allFilled = $('.po').filter(function () {
            return $.trim($(this).val()).length == 0;
        });

        if (allEmpty == tLength) {
            //All empty
        }
        else if (allFilled == tLength) {
            //All Filled
        }
相关问题