【问题标题】:iterate list accessed using session迭代使用会话访问的列表
【发布时间】:2016-02-26 05:53:38
【问题描述】:

我希望从 jquery 中的 java/grails 中迭代列表,如下所示

控制器

session.legendList = legendList // [Soham Shetty] list in controller set to session

普惠制

<input type="hidden" name="legendList" id="legendListId" value="${session.legendList}">

jQuery

var filterList= $("#legendListId").val();
// also tried
// var arrLegendList = jQuery.makeArray( filterList );      
for(f in filterList){
     $('#impact-report-user-filter-dropdown').append($('<option>', {
                     value: filterList[f],
                     text:  filterList[f]
     }));
}
$("#impact-report-user-filter-dropdown").multiselect("refresh"); 

这不起作用,它把 one char 的字符串作为 f 而不是 one element list

【问题讨论】:

  • 那么服务 &lt;input ...&gt; 是什么样子的?
  • &lt;input type="hidden" name="legendList" id="legendListId" value="[Soham Shetty]"&gt;

标签: jquery list session grails gsp


【解决方案1】:

客户端,$("#legendListId").val() 返回一个字符串,而不是一个可迭代的列表。

for(f in filterList) {...} 确实会迭代,但会遍历字符串的每个 字符,包括方括号,这不是您想要的。见fiddle

通过在&lt;input&gt; 元素中提供列表的CSV 表示,javascript/jQuery 将能够将$("#legendListId").val() 解析为带有String.prototype.split() 的数组。

或者,传递您的Groovy list to javascript directly

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    相关资源
    最近更新 更多