【问题标题】:Get all elements in a list or array using EL expressions in autocomplete combobox using jquery使用jquery在自动完成组合框中使用EL表达式获取列表或数组中的所有元素
【发布时间】:2017-06-07 11:16:38
【问题描述】:

我想使用 el 表达式动态获取列表或数组中的所有元素。

我正在使用以下代码。如果我使用变量 isstatus 它不起作用。

${claimNotificationForm.chattels[istatus].objectValue}

下面的工作就像..

${claimNotificationForm.chattels[0].objectValue}
${claimNotificationForm.chattels[1].objectValue}

如何在此处使用变量,以便根据状态值评估 el 表达式。

下面是我使用的jsp代码。

 _createAutocomplete: function() {

              var x = this.element[0].id;     //value is combobox-0
              var status = x.substr(x.length - 1);  // value is 0 which is in string
              var istatus = parseInt(status);  // converted to int


              this.input = $( "<input>" )
                .appendTo( this.wrapper )
                .attr( "title", '<fmt:message key="page.claim.personalclaimnotification.injury.select_info" />' )
                .val("${claimNotificationForm.chattels[0].objectValue}")    //works fine with 0,1,2... I have to use 'istatus'  here
                .css({
                    color: function( index, value ) {
                        if (this.value == '<fmt:message key="page.claim.search.object.name" />') {
                            return "#777";
                        }
                    },
                    fontStyle: function( index, value ) {
                        if (this.value == '<fmt:message key="page.claim.search.object.name" />') {
                            return "italic";
                        }
                    },
                    width: "286px"
                })
                .attr("maxlength", 256)
                .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
                .autocomplete({
                  delay: 0,
                  minLength: 3,
                  source: $.proxy( this, "_source" )
                })

【问题讨论】:

  • 我明白了。您可以尝试打印状态值并检查它是什么吗?要么使用 console.log(status);或警报(状态);我想我们会这样发现问题
  • 或者您可能会问另一个关于“JSTL 核心库/EL 表达式中的方括号在 jquery 代码中不起作用”的问题。类似的东西
  • 是的。会这样做。 @法拉兹

标签: javascript java jquery combobox el


【解决方案1】:

您根本不需要使用 []。只需运行一个 foreach 并放置一个 if/else 语句。

<c:forEach items="${ListName}" var="emp" varStatus="myIndex">
<c:choose>
  <c:when test="${myIndex.index==1}">
  //do something
  </c:when>

</c:forEach>

编辑:

在了解了有关要求的更多信息后,我继续进行了自己的测试。我的在这两种情况下都工作得很好。所以问题要么是 isstatus 正在评估某个超过列表大小的索引,要么我不知道。如果你运行&lt;c:out value="${istatus}" /&gt;,你能检查一下 istatus 的值是多少

当我为变量 istatus 设置一个随机值时,如果该值在列表的大小范围内,它会打印出一些东西,如果它大于列表的大小,它会打印出空白。这意味着它不会直接引发错误。

 <c:set var="istatus" value="${1}" />
 ...
 <c:out value="${claimNotificationForm.chattels[istatus].objectValue}" /> //it prints something meaning it is working

【讨论】:

  • 如何在不使用循环的情况下使用,因为我需要从任何索引的数组/集合中获取元素。这里索引存在于状态变量中。 ${claimNotificationForm.chattels[status].objectValue} @faraz
  • @Ramhakrishna 好的,我正在调查它
  • @Ramhakrishna 你可以尝试在某处 检查它显示的值吗?
  • @Ramhakrishna 你能告诉我们完整的jsp代码吗?那个状态是从哪里来的?谢谢
  • 我知道它是从哪里来的。但我不知道如何进一步提供帮助,因为我的知识库已经枯竭了。
猜你喜欢
  • 2012-01-28
  • 2014-11-27
  • 1970-01-01
  • 2017-08-17
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多