使用 Javascript 在 Acrobat 中复制字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8056046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:33:02  来源:igfitidea点击:

Copy field values in Acrobat using Javascript

javascriptpdfacrobat

提问by CaseyHunt

How can I copy the form field values from one set of fields to another using javascript.

如何使用 javascript 将表单字段值从一组字段复制到另一组字段。

The idea here is to have a 'use shipping/billing address' type of button that copies the user information from one block of fields to another identical set of fields.

这里的想法是有一个“使用送货/账单地址”类型的按钮,它将用户信息从一个字段块复制到另一组相同的字段。

Right now, I call an action upon click of a button to execute the following javascript:

现在,我在单击按钮时调用一个操作来执行以下 javascript:

this.field1.value = this.field2.value;

However that action yields an 'undefined' error in the debugger.

但是,该操作会在调试器中产生“未定义”错误。

回答by CaseyHunt

For posterity, this is the solution to the problem:

对于后代,这是解决问题的方法:

getField("field2").value = getField("field1").valueAsString;

Also, note that field2is set to field1so the order is backwards.

另外,请注意field2设置为field1所以顺序是倒序的。

回答by Gaelen

I used the following code to avoid overwriting the value in the second field if it has something in it already:

我使用以下代码来避免覆盖第二个字段中的值,如果它已经有内容的话:

//Set the source and destination vars:
      var source = this.getField("Box1");
      var destination = this.getField("Box2");

//See if destination is empty and if so, insert source value
      if(destination.value==''||destination.value==null){destination.value=source.value}

I used it on "On Blur" of the source Field, but you could use a button with "Mouse Up" as the trigger. (I found the code on this website. It includes more complicated options for populating multiple fields or even joining values from two source fields into one destination field.)

我在源字段的“On Blur”上使用它,但您可以使用带有“Mouse Up”的按钮作为触发器。(我在这个网站上找到了代码。它包含更复杂的选项,用于填充多个字段,甚至将两个源字段中的值连接到一个目标字段中。)