输入显示包含数据的表单字段的数量(jQuery)


Input show the number of form fields with data (jQuery)

我有一个长表单,我需要计算包含数据的输入和文本区域的数量。 我需要一个输入字段,该字段将显示一个数字,表示表单中包含数据的输入和文本区域的数量。

我更喜欢jquery或PHP。

有什么例子或建议吗?

谢谢。埃里克

使用 jQuery,你可以使用它

var number = $('#form').find('input, textarea')
                       // filter out every empty input/textarea
                       .filter(function() { return $(this).val() == ''; })
                       .length;

您可以执行一个 onkeypress 函数,该函数运行并使用包含数据的字段数更新您的div。

在我的头顶上,但也许:

function tallyFields() {
var totalFilledFields = 0;
var fieldAVal = document.getElementByID("fieldA").value;
// add more fields here the same way
if(fieldAVal.length > 0) {
 totalFilledFields++;
}
// check more fields here the same way and increment the same var
document.getElementByID("yourTotalDiv").innerHTML=totalFilledFields;
}

在每个表单字段的按键上分配此功能可能会解决问题。

这应该可以解决问题:

$(".myform :input").length;