如何将javascript添加到PDF文档以计算复选框的数量并将值输入文本框区域? [英] How can I add javascript to a PDF document to count the number of checked boxes and enter the value into a textbox area?

查看:136
本文介绍了如何将javascript添加到PDF文档以计算复选框的数量并将值输入文本框区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个PDF文档,有25个复选框,名为cb1到cb25。我希望能够计算被检查的盒子数量,并将该数量放入一个名为Points的文本框区域。

So I have a PDF doc that has 25 check boxes called "cb1" through "cb25". I would like to be able to count the number of boxes that are checked and put that count into a text box area called "Points".

我不是很熟悉无论是JS还是PDF表单创建,但从我能够挖掘出来的东西,我认为我已接近它的工作。

I'm not very familiar with either JS or PDF form creation but from what I've been able to dig up I think I'm close to getting it to work.

我添加了以下内容代码到文档级别:

I have added the following code to the document level:

function CountCheckBoxes(aFieldsNames) {
    // count field names that have been selected
    var count = 0;
    // loop through array of field names
    for (i = 0; i < aFieldNames.length; i++) {
      // for field names with a value of not Off increment counter
      if (this.getField(aFieldNames[i]).value != "Off") count++;
    } // end loop of field names
    // return count
    return count;
  } // end CountCheckBoxes

我尝试添加以下代码用于在鼠标上执行JS的文本框属性以及作为计算值,这些属性似乎都不能用复选框的计数填充文本框。

I've tried adding the following code text box properties to execute JS on mouse up and as a calculated value, neither of which seem to work to populate the text box with a count of checked boxes.

// var define field names to be tested
var aFields = new Array('cb1', 'cb2', 'cb3', 'cb4', 'cb5', 'cb6', 'cb7', 'cb8', 'cb9', 'cb10', 'cb11', 'cb12', 'cb13', 'cb14', 'cb14', 'cb15', 'cb16', 'cb17', 'cb18', 'cb19', 'cb20', 'cb21', 'cb22', 'cb23', 'cb24', 'cb25');
// count field names that have been selected
event.value = CountCheckBoxes(aFields);

推荐答案

下面的代码应添加到文本f中保持计数框的字段。为此,右键单击表单域,然后单击属性 - >计算 - >自定义计算脚本 - >编辑...。

The code below should be added to the text field that keeps count of the boxes. To do so, right click on the form field then Properties -> Calculate -> Custom Calculation Script -> "Edit...".

var sum = 0;
for ( i = 1; i < 26; i++ ) {
        f = "cb" + i;
        field = getField(f);
        if (field.isBoxChecked(0)) {
            sum = sum + 1;
        }
    }
event.value = sum;

这是在实际文档中测试和使用的。以下是有关代码的一些详细信息:

This is tested and working in an actual document. Here are some details about the code:

有一个遍历所有25个字段的循环,并为每个字段创建一个字符串。字符串值为cb1,cb2等。然后按名称获取字段。 isBoxChecked(0)字段方法,如果选中此框,则返回true。如果选中一个框,则代码将增加所有已检查字段的总和。全部完成后,总和将分配给当前文本字段。

There is a loop that goes over all 25 fields, and creates a string for each one of their names. The string values are "cb1", "cb2" etc. Then gets the field by name. The isBoxChecked(0) field method, will return true if the box is checked. If a box is checked, the code will bump up the sum of all checked fields. When it's all done, the sum is assigned to the current text field.

这是JS for Acrobat参考的链接。将上面的样本放在一起时非常有用。

Here is a link to the JS for Acrobat reference. It's quite useful when putting together samples like the one above.

这篇关于如何将javascript添加到PDF文档以计算复选框的数量并将值输入文本框区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆