我在 Acrobat 中打开的 Adobe PDF 中有几个复选框。我想计算已选中的复选框并将数字输出到另一个文本框中。我怎样才能用 JavaScript 做到这一点?或者其他方法?
最佳答案
一种可能是设置一个计数器,枚举所有字段,检查该字段是否为复选框,然后检查其值是否不为“Off”;在这种情况下,增加计数器。执行此操作的函数如下所示:
function countCheckedBoxes() {
var cntr = 0;
for (var i = 0; i < this.numFields; i++) {
var mf = this.getField(this.getNthFieldName(i));
if (mf.type == "checkbox") {
if (mf.value != "Off") {
cntr++;
}
}
}
if (cntr > 0) {
app.alert("The form has " + cntr + " checked boxes")
} else {
app.alert("The form has no checked boxes")
}
}
这样就可以了。
由于复选框的数量应输入另一个字段,因此您可以使用以下代码替换 cntr
的计算:
if (cntr > 0) {
this.getField("boxCount").value = cntr + " boxes checked";
} else {
this.getField("boxCount").value = "no boxes checked";
}
...或者直接输入 cntr
的值。
关于javascript - 如何只计算 PDF 表单中选中的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24624092/