JSON如何从Javascript对象中的值在相同索引处查找另一个值 [英] JSON how find another value at the same index from a value in Javascript Object
问题描述
我确定这是一个简单的问题,但我无法弄清楚.
A simple question I'm sure, but I can't figure it out.
我从服务器返回了一些JSON
I have some JSON returned from the server
while ($Row = mysql_fetch_array($params))
{
$jsondata[]= array('adc'=>$Row["adc"],
'adSNU'=>$Row["adSNU"],
'adname'=>$Row["adname"],
'adcl'=>$Row["adcl"],
'adt'=>$Row["adt"]);
};
echo json_encode(array("Ships" => $jsondata));
...我在ajax调用的客户端上使用.应该注意的是,JSON被解析为一个全局声明的对象,以便以后使用.我假设您知道我正确格式化了ajax调用的格式...
...which I use on the client side in an ajax call. It should be noted that the JSON is parsed into a globally declared object so to be available later, and that I've assumed that you know that I formated the ajax call properly...
if (ajaxRequest.readyState==4 && ajaxRequest.status==200 || ajaxRequest.status==0)
{
WShipsObject = JSON.parse(ajaxRequest.responseText);
var eeWShips = document.getElementById("eeWShipsContainer");
for (i=0;i<WShipsObject.Ships.length;i++)
{
newElement = WShipsObject.Ships;
newWShip = document.createElement("div");
newWShip.id = newElement[i].adSNU;
newWShip.class = newElement[i].adc;
eeWShips.appendChild(newWShip);
} // end for
}// If
例如,您可以在这里看到我已经在父div内创建了HTML DIV元素,每个新div都有一个id和一个类.您还将注意到,我还没有使用对象中返回的所有数据...
You can see for example here that I've created HTML DIV elements inside a parent div with each new div having an id and a class. You will note also that I haven't used all the data returned in the object...
我使用JQuery来处理对对象的单击,这是我的问题,我要使用的是元素的ID以返回另一个值,例如,来自同一索引处JSON的adt
值.麻烦的是,在单击事件中,我不再知道索引,因为它是在元素创建之后出现的.即我不再处于for
循环中.
I use JQuery to handle the click on the object, and here is my problem, what I want to use is the id from the element to return another value, say for example adt
value from the JSON at the same index. The trouble is that at the click event I no longer know the index because it is way after the element was created. ie I'm no longer in the for
loop.
那我该怎么做呢?
这是我尝试过的方法,但是我认为我在错误的树上...在两个测试用例中,.inArray()
返回负1.记住该对象是全局可用的...
Here's what I tried, but I think I'm up the wrong tree... the .inArray()
returns minus 1 in both test cases. Remember the object is globally available...
$(".wShip").click(function(){
var test1 = $.inArray(this.id, newElement.test);
var test2 = $.inArray(this.id, WShipsObject);
//alert(test1+"\n"+test2+"\n"+this.id);
});
推荐答案
我无法找到一种按要求查找索引的方法,但是我在Devraj的答案上创建了一个变体.
I could not find a way of finding the index as asked, but I created a variation on the answer by Devraj.
在解决方案中,我创建了一个名为key
的自定义属性,在其中存储了索引.
In the solution I created a custom attribute called key
into which I stored the index.
newWShip.key = i;
稍后,当我再次需要索引时,可以在JQuery .click()
方法中使用this.key
:
Later when I need the index back again I can use this.key
inside the JQuery .click()
method:
var key = this.key;
var adt = WShipsObject.Ships[key].adt;
您可能会争辩说实际上我可以将所有数据存储到自定义属性中,但是我会争辩说那将是不必要的内存重复.
You could argue that in fact I could store all the data into custom attributes, but I would argue that that would be unnecessary duplication of memory.
这篇关于JSON如何从Javascript对象中的值在相同索引处查找另一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!