我是在Adobe Acrobat DC的一个可填写的PDF表格和新的JavaScript工作。我需要的价值是100美元,直到2018年9月21日,然后从9/22 - 10/19是125美元,然后从10/20开始是150美元。
我有下面的脚本,它适用于第一个if
语句,但它不会计算脚本的10/20/2018部分。有没有人能帮帮我,告诉我哪里做错了?
var sub = 100 * Number(this.getField("numEthernet").value);
var s = this.getField("Date").valueAsString;
if (s!="") {
var d = util.scand("mm/dd/yyyy", s);
var cutOffDate = util.scand("mm/dd/yyyy", "9/21/2018");
if (d.getTime()>cutOffDate.getTime()){
sub *= 1.25;
}
}
else if (s!="") {
var d = util.scand("mm/dd/yyyy", s);
var cutOffDate = util.scand("mm/dd/yyyy", "10/20/2018");
if (d.getTime()>=cutOffDate.getTime()){
sub *= 1.50;
}
}
event.value = sub;
发布于 2018-07-03 03:07:00
我不熟悉Acrobat DC,所以我不太确定javascript的一些基本方法/对象的可用性,但这应该可以工作,因为我试图从我的答案中剔除任何不必要的代码:
var sub = 100 * Number(this.getField("numEthernet").value);
var s = this.getField("Date").valueAsString;
if (s != "") {
var dateFormat = "mm/dd/yyyy";
var suppliedDate = util.scand(dateFormat, s).getTime();
if (suppliedDate >= util.scand(dateFormat, "9/22/2018").getTime() && suppliedDate <= util.scand(dateFormat, "10/19/2018").getTime()){
sub *= 1.25;
}
else if (suppliedDate >= util.scand(dateFormat, "10/20/2018").getTime()) {
sub *= 1.50;
}
}
event.value = sub;
将来我会建议用类似于var s = this.getField("Date").valueAsString.trim()
的东西替换var s = this.getField("Date").valueAsString
,这样空格就不会引起任何问题,但我不确定trim在Acrobat DC中是否可用
发布于 2018-07-03 02:41:26
看起来您的if语句正在检查s是否不是空字符串。您的else if语句也在寻找相同的东西,但是由于您的初始if语句已经成功,它将不会查找else if。
在不知道确切语法的情况下,尝试查找两个项目:
if(s!="" && /* Check if the current date is within your first time period */) {
sub *= 1.25;
}
else if(s!="" && /* Check if the current date is within your second time period */) {
sub *= 1.50;
}
一些类似的东西。
https://stackoverflow.com/questions/51141846
复制相似问题