document.getElementById
预期结果:仅使用 javascript在加载时自动填写表单。
我有一个传递给以下函数的数组,该函数正确地将其添加到 html 脚本中。
function showResults(results) {
var s = "";
for (var i=0, il=results.features.length; i<il; i++) {
var featureAttributes = results.features[i].attributes;
for (att in featureAttributes) {
s = s + "<b>" + att + ":</b> " + featureAttributes[att] + "<br />";
// var subshed = feature[0].attributes["SubWS"];
}
}
dojo.byId("info").innerHTML = s;
}
结果看起来像
对象编号:190 ProjectTyp:雨水改造 单位:下 SubWS:悲伤
但是,我需要修改脚本以填写现有表格
document.getElementById(att).value = featureAttributes[att];
我似乎总是覆盖最终输出,所以我只填写一个表单值。如何填写多个值?
更新:Polywhirl 先生做对了。我还必须添加一个 if 语句来处理空属性值。最终代码是:
function showResults(results) {
var subshed = results.features[0].attributes["SubWS"];
//OBJECTID = results.features[0].attributes["OBJECTID"];
document.getElementById("searchString").value = subshed;
console.log("feature", results.features[0].attributes["SubWS"])
console.log(document.getElementById("Map_ID"));
var s = "";
for (var i = 0, il = results.features.length; i < il; i++) {
var featureAttributes = results.features[i].attributes;
for (att in featureAttributes) {
// s = s + "<b>" + att + ":</b> " + featureAttributes[att] + "<br />";
// var subshed = feature[0].attributes["SubWS"];
// document.getElementById(att).value = featureAttributes[att];
if(!!featureAttributes[att]&&!!document.getElementById(att)){
document.getElementById(att).value = featureAttributes[att];
}else{
//alert(att + "this is null")
}
}
}
// dojo.byId("info").innerHTML = s;
}