在 for 循环中使用哈希值 jQuery
Using Hashids in a for loop jQuery
我正在尝试使用 hashids 插件在 for 循环中散列 ticket_id
。
for (var i = 0; i < result.length; i++) {
var hashids = new Hashids("", 5);
var id = hashids.encode(result[i].ticket_id);
console.log(hashids);
[...]
html += '<tr class="ticketClick" data-url="' + baseURL + id>' [...]
}
HTML 对于 table 是根据 ajax 调用传递的 result
动态生成的。单击 table 行时,我重定向到 data-url 属性中的 url。
问题是 hashids 没有 运行,因此 id
没有附加到 url。
hashids
的控制台日志产生:
Hashids {version: "1.0.2", minAlphabetLength: 16, sepDiv: 3.5, guardDiv: 12, errorAlphabetLength: "error: alphabet must contain at least X unique characters"…}
我已经搜索了错误,但没有找到任何东西...
我觉得奇怪的是,如果我将一个整数直接传递给 hashids 方法,例如:
var id = hashids.encode(20);
编码有效,returns 预期的哈希值。
A console.log of result[i].ticket_id
returns 预期的整数,所以 for 循环正在正确地完成它的工作。在 for
循环之外声明 new Hashids()
似乎没有什么不同。所以我不确定这里出了什么问题。有什么建议吗?
问题是由 Ajax 函数以 JSON 格式提取数据引起的。
解决方案
将值解析为整数:
parseId(result[i].ticket_id)
我正在尝试使用 hashids 插件在 for 循环中散列 ticket_id
。
for (var i = 0; i < result.length; i++) {
var hashids = new Hashids("", 5);
var id = hashids.encode(result[i].ticket_id);
console.log(hashids);
[...]
html += '<tr class="ticketClick" data-url="' + baseURL + id>' [...]
}
HTML 对于 table 是根据 ajax 调用传递的 result
动态生成的。单击 table 行时,我重定向到 data-url 属性中的 url。
问题是 hashids 没有 运行,因此 id
没有附加到 url。
hashids
的控制台日志产生:
Hashids {version: "1.0.2", minAlphabetLength: 16, sepDiv: 3.5, guardDiv: 12, errorAlphabetLength: "error: alphabet must contain at least X unique characters"…}
我已经搜索了错误,但没有找到任何东西...
我觉得奇怪的是,如果我将一个整数直接传递给 hashids 方法,例如:
var id = hashids.encode(20);
编码有效,returns 预期的哈希值。
A console.log of result[i].ticket_id
returns 预期的整数,所以 for 循环正在正确地完成它的工作。在 for
循环之外声明 new Hashids()
似乎没有什么不同。所以我不确定这里出了什么问题。有什么建议吗?
问题是由 Ajax 函数以 JSON 格式提取数据引起的。
解决方案
将值解析为整数:
parseId(result[i].ticket_id)