所以我有一个表格,你会得到你完成的表格的百分比。问题出在其中一个输入上,即使它是空的,我也会得到一个结果。我不确定如何解决这个问题。这是代码
countMissing();
function countMissing() {
//Get total inputs
console.log("Total inputs " + form.getElementsByTagName('input').length);
//Divide by complete inputs out of 100% and get percent
console.log("The percentage is " + 100 / form.getElementsByTagName('input').length + "%");
var tot = 100 / form.getElementsByTagName('input').length + "%"
//Check
var cback=function(){
bad=0;
$('.form :text').each(function (i,e) {
if ($.trim($(e).val()) == "") bad++;
//Change width
$('.top').css('width', bad + '%');
});
//Missing amount of fields
if (bad > 0) $('.congrats').css("display", "block").text(100/bad + ' Completed ');
else $('.congrats').hide();
}
$(document).delegate('.form :text','focus',cback);
$(document).delegate('.form :text','keyup',cback);
}
以及演示链接 http://jsbin.com/xijemuli/2/edit有什么想法吗?
最佳答案
FIDDLE:http://jsfiddle.net/abdennour/3mH6w/3/
function count(){
console.log("Total inputs " + $('.form :text').length);
//Divide by complete inputs out of 100% and get percent
console.log("The percentage is " + bad / $('.form :text').length + "%");
return 100-(bad / $('.form :text').length)*100 + "%"
}
更新:
另一个具有 10 个输入形式的示例:http://jsfiddle.net/abdennour/3mH6w/6/
关于javascript - 空输入算满,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23588233/