[英]How to iterate list inside hashtable key with javascript
I have a list of strings (List) that I populated with pictures(in my code-behind). 我有一个用图片填充的字符串列表(列表)(在我的代码后面)。 Then I store it in a Hashtable with a key of "sbPrints" which then gets returned to the ajax call.
然后,将其存储在带有“ sbPrints”键的哈希表中,然后将其返回给ajax调用。
However I don't know how to iterate the list inside that specific key in javascript. 但是我不知道如何在javascript中的特定键内迭代列表。
the hastable containing the list in javascript looks like this : h['sbPrints'].
包含javascript中列表的hastable看起来像这样:
h['sbPrints'].
the reason I'm asking about this is because if I just do 我问这个的原因是因为如果我只是做
$('#prints').val(h['sbPrints']);
then I end up with unwanted commas in between each picture. 然后我在每张图片之间都会出现不必要的逗号。
EDIT:how the hashtable gets populated 编辑:如何填充哈希表
iny my code-behind: 在我的代码背后:
[WebMethod]
public static Hashtable getPersonInfo(int personID)
{
Hashtable h = new Hashtable();
SqlDataReader drThumbs;
drThumbs = comGetThumbs.ExecuteReader();
List<string> fingerPrints = new List<string>();
while(drThumbs.Read())
{
fingerPrints.Add("<div class=\"fingerprints\"><img alt='prints' src='../ShowThumbnail.ashx?BFID=" + drThumbs["BinaryFileID"].ToString() + "'/><div><label><a class=\"finger\" href='../DownloadFile.aspx?id=" + drThumbs["BinaryFileID"].ToString() + "'>" + drThumbs["FileName"].ToString() + "</label></div></div>");
}
}
Then in my javascript file I make an ajax call to the the method and populate my asp controls with the data from the hashtable. 然后在我的javascript文件中,我对该方法进行ajax调用,并使用哈希表中的数据填充我的asp控件。
I don't quite understand what you mean, but how about this: 我不太明白您的意思,但是这是怎么回事:
$.each(h['sbPrints'], function(i, v) {
$('#prints').val(v);
});
This will iterate through every item in h['sbPrints']
and pass them to your prints
selector's jQuery value function. 这将遍历
h['sbPrints']
每个项目,并将它们传递给prints
选择器的jQuery值函数。 I don't really know why you would want to do this, as $.each
will replace the value in each iteration. 我真的不知道为什么要这么做,因为
$.each
将在每次迭代中替换该值。
Perhaps what you mean is that h['sbPrints']
is an array and when it's stringified, your browser will insert commas between the items. 也许您的意思是
h['sbPrints']
是一个数组,将其字符串化时,您的浏览器将在项目之间插入逗号。 If you just want the list without commas, then you can do this: 如果只希望列表不带逗号,则可以执行以下操作:
$('#prints').val(h['sbPrints'].join(''));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.