只有输入文本才能正常工作

时间:2014-05-11 16:49:16

标签: javascript jquery html

所以我有一个脚本可以获得输入的总数并得到剩余的百分比来完成。但我的脚本只获得type="text"。关于如何解决这个问题的任何想法?这是我的代码

var bad = 0;
var cback=function(){
    bad=0;
    $('.form :text').each(function (i,e) {
        if ($.trim($(e).val()) == "") bad++;
    });
    $('.congrats').css("display", "block").text(bad + ' missing(Completed '+count()+')');
     //else $('.congrats').hide();

     $(".top").css("width", 100-(bad / $('.form :text').length)*100 + "%");
}
$(document).delegate('.form :text','focus',cback);
$(document).delegate('.form :text','keyup',cback)

function count(){
    return 100-(bad / $('.form :text').length)*100 + "%"
};

那我怎么能有type="password"等等呢?我是否为所有这些代码执行相同的代码?什么是你的方法?

4 个答案:

答案 0 :(得分:0)

尝试

Attribute Equals Selector [name="value"]

$('.form [type="password"]') //selector for type="password" inside element with form class 

$('.form :text, .form [type="password"]')//select type="text" and type="password"

答案 1 :(得分:0)

您可以使用以下选择器

获取所有输入
$('.form input')

答案 2 :(得分:0)

$('.form input').not(':input[type=submit]');

看小提琴结果: http://jsfiddle.net/NtL6f/

答案 3 :(得分:0)

你这样做

var inputs = $('.form :input').not('button, [type="button"], [type="submit"]').on({input: cback});

function cback() {
    var bad = inputs.filter(function() {
        return !this.value.length;
    }).length,
        total = inputs.length;

    $('.congrats').show().text(bad + ' missing(Completed ' + ((100 - (bad / total) * 100).toFixed(2) + "%") + ')');
    $(".top").css("width", 100 - (bad / total) * 100 + "%");
}

FIDDLE