Javascript 仅当字段为空时才用今天的日期填写 Acrobat 表单日期字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3261712/
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
Fill Acrobat Form date field with today's date only if field is empty
提问by Wells Anderson
The goal is for an Acrobat form to fill a date field ("MeetingDate") with Today's date when the user opens it. If the user saves the form as is and opens it a few days later, the old date should still be there, not replaced by the current date. If the user changes date in the field, that date should be saved and should not be replaced when the form is opened later.
目标是让 Acrobat 表单在用户打开它时用今天的日期填充日期字段(“MeetingDate”)。如果用户按原样保存表单并在几天后打开它,旧日期应该仍然存在,而不是被当前日期替换。如果用户在字段中更改日期,则应保存该日期,并且在稍后打开表单时不应替换该日期。
I have a Custom Calculation Script for a date field ("MeetingDate") that does all of this, but with one problem:
我有一个日期字段(“MeetingDate”)的自定义计算脚本可以完成所有这些,但有一个问题:
if (event.value != "")
then
event.value = util.printd ("m/d/yyyy", new Date())
endif
This works well except that after entering the script, today's date fills in the MeetingDate field and the value is saved. That makes sense, but I want the field to be automatically filled with Today's date when the user opens the form. Instead, if the user opens the form tomorrow (7/16/2010) it will have 7/15/2010 in the field because that is the date I saved the form.
这很有效,只是在输入脚本后,今天的日期会填入 MeetingDate 字段并保存该值。这是有道理的,但我希望该字段在用户打开表单时自动填充今天的日期。相反,如果用户明天(2010 年 7 月 16 日)打开表单,则字段中将显示 7/15/2010,因为那是我保存表单的日期。
I think the answer may be to enter a script as a Document JavaScript (Advanced | Document Process | Document JavaScripts), since Document scripts execute when the form is opened, but all my attempts cause a date entered by the user to be overwritten when the form is opened days later. Thanksfor the help!
我认为答案可能是将脚本作为文档 JavaScript(高级 | 文档处理 | 文档 JavaScript)输入,因为文档脚本在表单打开时执行,但我所有的尝试都会导致用户输入的日期在几天后打开表格。感谢您的帮助!
回答by o-o
Remove your script and replace it with the following document javascript:
删除您的脚本并将其替换为以下文档 javascript:
var f = this.getField("wells_datefield");
if (!f.value) f.value = util.printd ("m/d/yyyy", new Date());
Save your form and open it, and the date should pop right in there.
保存您的表单并打开它,日期应该会在那里弹出。
When making the document javascript, remember to not put it inside a function. One way of doing that is the following. In the Acrobat menu:
在制作文档 javascript 时,记住不要把它放在函数中。这样做的一种方法如下。在 Acrobat 菜单中:
- Advanced->Document Processing->Document Javascripts...
- In the dialog that pops up, enter a Script Name (for instance "populate_date") and click Add...
- In the script dialog that pops up: REMOVE everything ("function populate_date(){}") and paste in the script above.
- Click ok, then Close.
- Save the form, close it and reopen it.
- There is no step 6! :>
- 高级->文档处理->文档Javascripts...
- 在弹出的对话框中,输入脚本名称(例如“populate_date”)并单击添加...
- 在弹出的脚本对话框中:删除所有内容(“function populate_date(){}”)并粘贴上面的脚本。
- 单击确定,然后关闭。
- 保存表单,关闭它并重新打开它。
- 没有第6步!:>
回答by Omar
@wells-anderson
@威尔斯-安德森
You just need to remove the if statement:
您只需要删除 if 语句:
event.target.value = util.printd("m/d/yyyy", new Date());
回答by Puzznic
Hope you don't mind if I add this edit of your code for British users (and newbies like me)
希望您不介意我为英国用户(以及像我这样的新手)添加此代码编辑
var f = this.getField("date");
if (!f.value) f.value = util.printd ("dd/mmm/yyyy", new Date());
This code fills the date field as follows...
此代码按如下方式填充日期字段...
03/Jan/2013
回答by Gandoe Dyck
Unfortunately, when I add var f = this.getField("DatepPrinted"); if (!f.value) f.value = util.printd ("dd/mmmm/yyyy", new Date()); to my custom keystroke on a form it gives me back "0" instead of 02-april-2015..anyone know why?
不幸的是,当我添加 var f = this.getField("DatepPrinted"); if (!f.value) f.value = util.printd ("dd/mmmm/yyyy", new Date()); 对于我在表单上的自定义击键,它返回“0”而不是 02-april-2015 ..谁知道为什么?

