如何动态设置“必选”Acrobat中字段的状态

How do I dynamically set the "required" status of fields in Acrobat?

本文关键字:Acrobat 字段 状态 必选 何动态 动态 设置      更新时间:2023-09-26

我有一个Acrobat PDF,一个同事希望在Acrobat提供的简单选项之外添加一些表单验证,而不使用javascript。据我所知,这需要Javascript来完成。我认为行为应该能够通过"添加一个动作->MouseDown->运行一个Javascript"选项找到单选按钮组的属性->操作来完成。

基本上我有一组3个按钮的单选按钮。如果按下了按钮a,我希望字段a是必需的。如果按下按钮b,我希望字段db是必需的。buttonC/fieldC也一样。

我知道我的伪代码会是什么样子,但我有麻烦把它翻译成javascript

onSelectionChanged(selection) {
    fieldA.required = false;
    fieldB.required = false;
    fieldC.required = false;
    if (selection == 'A') { fieldA.required = true; }
    if (selection == 'B') { fieldB.required = true; }
    if (selection == 'C') { fieldC.required = true; }
}
谁能给我指个正确的方向?

做了更多的挖掘,这是最终的解决方案(放在单选按钮组的"MouseUp->Run a Javascript"中:

var f = this.getField("ServiceType");
var ins = this.getField("InstallationDateTime");
var rem = this.getField("RemovalDateTime");
var ser = this.getField("ServiceRequestDateTime");
console.println(f.value);
ins.required = false;
rem.required = false;
ser.required = false;
if (f.value === "Install") {
    ins.required = true;       
}
if (f.value === "Removal") {
    rem.required = true;       
}
if (f.value === "Service") {
    ser.required = true;       
}

考虑到所有因素都很简单,找到以下内容对调试非常有用:

f = this.getField("myField");
for ( var i in f ) {
    try {
        if ( typeof f[i] != "function" ) {    // Do not list field methods
            console.println( i + ":" + f[i] ) 
        }
    } catch(e) {
    }           // An exception occurs when we get a property that does not apply to this field type.
}