我有一个JavaScript代码,可从json中获取对象。
由此,我建立了一个HTML字符串:
var htmlstr = "<table border=0>";
for (var i=0;i<jsonData.people.length;i++) {
htmlstr=htmlstr+"<tr><td>" + jsonData.people[i].name + "</td>";
htmlstr=htmlstr+"<td>"+ jsonData.people[i].cash + "</td>";
htmlstr=htmlstr+"<td><button onclick='changeCash(i)'>Update</button></td></tr>";
}
htmlstr=htmlstr+"</table>";
layer.addFeatures(features);
layer.events.on({ "featureselected": function(e) { updateMak('mak', htmlstr) } });
function changeCash(k) {
jsonData.people[k].cash=jsonData.people[k].cash+100;
}
HTML页面如下:
<script type="text/javascript">
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
alert("Mobile device detected."); }
function updateMak(id,content) {
var container = document.getElementById(id);
container.innerHTML = content;
} </script>
<div id="mak"> List of People </div>
可以说这显示了10个人的钱。如果我单击其更新按钮之一,则认为json数据已按预期进行了更新。但我不知道如何验证。表单中的值不会通过changeCash函数更新新值。
如何更新htmlstr并更新屏幕上已经显示的内容?
请指教。
当您为人现金生成htmlstr时
htmlstr=htmlstr+"<td>"+ jsonData.people[i].cash + "</td>";
您还应该为此td生成ID,以便可以从功能changeCash(k)更新内容。
就像是
htmlstr=htmlstr+"<td id='peoplecash"+i+"'>" + jsonData.people[i].cash + "</td>";
然后在你的changeCash函数中
function changeCash(k) {
jsonData.people[k].cash=jsonData.people[k].cash+100;
var peoplecash= document.getElementById("peoplecash"+k);
peoplecash.innerHTML = jsonData.people[k].cash;}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句