我试图理解这一点,但由于某种原因我无法理解。 我对 Javascript 还很陌生......
这就是我想做的。
var companyList = {};
companyList.comanyZero = {
fldStreet: "That street 0",
fldPostcode: "0000 AA",
fldCity: "Amsterdam",
fldCountry: "The Netherlands"
};
companyList.companyOne = {
fldStreet: "Street name 1",
fldPostcode: "1234 BA",
fldCity: "Amsterdam",
fldCountry: "The Netherlands"
};
if (!event.willCommit){
if (event.changeEx === "companyZero"){
this.getField("fldStreet").value = companyList.companyZero.fldStraat;
this.getField("fldPostcode").value = companyList.companyZero.fldPostcode;
this.getField("fldCity").value = companyList.companyZero.fldCity;
this.getField("fldCountry").value = companyList.companyZero.fldCountry;
}
if (event.changeEx === "companyOne"){
this.getField("fldStreet").value = companyList.companyOne.fldStreet;
this.getField("fldPostcode").value = companyList.companyOne.fldPostcode;
this.getField("fldCity").value = companyList.companyOne.fldCity;
this.getField("fldCountry").value = companyList.companyOne.fldCountry;
}
}
这个想法是创建一个循环,根据组合框中选择的内容返回值。
请注意,这是 PDF 审核,因此用户可以更改 PDF 中的组合框,并且字段将填充正确的数据。
最佳答案
我认为您只是在寻找括号符号:
if (!event.willCommit) {
var currentCo = companyList[event.changeEx];
if (currentCo) {
this.getField("fldStreet").value = currentCo.fldStraat;
this.getField("fldPostcode").value = currentCo.fldPostcode;
this.getField("fldCity").value = currentCo.fldCity;
this.getField("fldCountry").value = currentCo.fldCountry;
}
}
如果 event.changeEx
包含 "companyOne"
,则 companyList[event.changeEx]
将引用 companyList.companyOne
.
由于您的属性名称和字段名称相同,您甚至可以维护要获取的键列表并使用循环更新其值:
var fieldNames = ["fldStreet", "fldPostcode", "fldCity", "fldCountry"]
if (!event.willCommit) {
var currentCo = companyList[event.changeEx];
if (currentCo) {
fieldNames.forEach((name) => {
this.getField("").value = currentCo[name];
})
}
}
关于javascript - 在 Javascript 中循环对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43937269/