javascript从json中获取元素id

标签 javascript php json

我有一个(我想很简单)引号、单引号、双引号的问题。 我有一个 JS 将数据发送到一个 php 文件,它响应用 json 发回一些数据。在下面的代码中,row.Dispon 是响应的一部分(并且工作正常)。但我想在 getElementById 中“回显”row.Element 但没有成功。我试过 "+row.Element+""'+row.Element+'"。我做错了什么?

if (row.Dispon=="ImageReload") {
  var text='Image changed';   
  document.getElementById(+row.Element+).value="due";
}

最佳答案

仅考虑您的代码片段,这应该可以解决该特定问题:

if (row.Dispon == "ImageReload") {
  var text = 'Image changed';   
  document.getElementById(row.Element).value = "due";
}

如果您尝试构建字符串,则需要引号(或双引号)和+ 运算符。看这个例子:

var id = 42;
document.getElementById('myId' + id).value = 'something';

假设 row.Element 已经包含一个 string,您可以直接将它传递给 getElementById()

这里有一些建议:

关于javascript从json中获取元素id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31677989/

相关文章:

javascript - 对于大量变量来说哪个更好

php - IE6位置固定问题

java - 线程 "main"java.lang.NoClassDefFoundError : com/fasterxml/jackson/annotation/JsonAutoDetect Error when trying to convert json to POJO 中的异常

ruby-on-rails - as_json 没有在关联上调用 as_json

php - 使用javascript获取表单中最接近的元素

json - TSQL FOR JSON 嵌套值

javascript - 如何使用流指定类类型参数?

javascript - 我的redis初始化函数可能导致内存泄漏

javascript - 带回调的 Bootstrap 通用模式

php - Propel - 使用多个外键连接同一个表