更好的JavaScript编码 - 您的建议是什么?

时间:2013-07-11 19:45:58

标签: javascript checkbox readonly acrobat

在编程方面,我是一个完整的菜鸟。我在Acrobat中为一个复选框编写了这个脚本,并希望从社区中了解更好,更简洁的方式来编写它。 TIA !!!

if (event.target.value == "Yes")

{
    this.getField("b.address").value = this.getField("a.address").value

    this.getField("b.address").readonly = true;

    this.getField("b.city").value = this.getField("a.city").value

    this.getField("b.city").readonly = true;

    this.getField("b.st").value = this.getField("a.st").value

    this.getField("b.st").readonly = true;

    this.getField("b.zip").value = this.getField("a.zip").value

    this.getField("b.zip").readonly = true;

} else

{
    this.getField("b.address").readonly = false;

    this.getField("b.address").value = "";

    this.getField("b.city").readonly = false;

    this.getField("b.city").value = "";

    this.getField("b.st").readonly = false;

    this.getField("b.st").value = "";

    this.getField("b.zip").readonly = false;

    this.getField("b.zip").value = "";

 }

1 个答案:

答案 0 :(得分:2)

var fields = ["address", "city", "st", "zip"];
var is_yes = event.target.value == "Yes";

fields.forEach(function(field) {
    var to = this.getField("b." + field);
    to.value = is_yes ? this.getField("a." + field).value : "";
    to.readonly = is_yes;
}, this);