我有一个 Acrobat PDF,一位同事希望在 Acrobat 不使用 JavaScript 的情况下为您提供的简单选项之外添加一些表单验证。据我所知,需要 JavaScript 来完成此操作。我认为该行为应该能够通过单选按钮组的“属性”->“操作”中的“添加操作->鼠标按下->运行 Javascript”选项来完成。
基本上我有一组带有 3 个按钮的单选按钮。如果按下buttonA,我希望需要fieldA。如果按下buttonB,我希望需要fieldB。 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;
}
考虑到所有因素,很容易找到 following非常适合调试:
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.
}
关于javascript - 如何在 Acrobat 中动态设置字段的 "required"状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15692435/