PDF Javascript - 设置字段值

时间:2017-05-20 21:09:33

标签: javascript pdf

我刚刚进入Javascript,我正在为一个rpg我和一些朋友的角色表编辑pdf,以方便使用。我希望它能自动填写表格 - 到目前为止我已经让它工作了,但是我想知道是否有一种方法可以压缩我的代码并减少重复次数?

var one = this.getField("Stat.0.0");
var statfield = this.getField("Text100.0.1");

if(one.value=="1"){
statfield.value="-30";
}
if(one.value=="2"){
statfield.value="-20";
}
if(one.value=="3"){
statfield.value="-10";
}
if(one.value=="4"){
statfield.value="-5";
}
if(one.value=="5"){
statfield.value="0";
}
if(one.value=="6"||one.value=="7"){
statfield.value="5";
}
if(one.value=="8"||one.value=="9"){
statfield.value="10";
}
if(one.value=="10"){
statfield.value="15";
}
if(one.value=="11"||one.value=="12"){
statfield.value="20";
}
if(one.value=="13"||one.value=="14"){
statfield.value="25";
}
if(one.value=="15"){
statfield.value="30";
}
if(one.value=="16"||one.value=="17"){
statfield.value="35";
}
if(one.value=="18"||one.value=="19"){
statfield.value="40";
}
if(one.value=="20"){
statfield.value="45";
}

有关清理此事的任何提示吗?

1 个答案:

答案 0 :(得分:1)

使用switch()代替if语句。不要忘记break

var one = this.getField("Stat.0.0").value();
var statfield = this.getField("Text100.0.1");

var oneval = one.value;
switch (oneval) {
    case "1":
        statfield.value = "-30";
        break;
    //case...
    case ("1" || "7"):
        statfield.value = "5";
        break;
    //case...
    default:
        break;
}

或(仅在break上注意case "7"):

switch (oneval) {
    //case...
    case "1":
    case "7":
        statfield.value = "5";
        break;
    //case...
}