【问题标题】:How do I set the selected item from p:selectOneListBox using javascript/jquery如何使用 javascript/jquery 从 p:selectOneListBox 设置所选项目
【发布时间】:2018-09-24 11:30:42
【问题描述】:

我正在尝试使用按钮动态设置 selectOneListbox 中的选定项目。

xhtml

<p:selectOneListbox  id="basic1" widgetVar='commmentWidget'  styleClass="commentClass" value="#{decisionTreeBean.option1}" style=" font-size: 12px;background-color:white;width:17em; height:13em; border-style:solid !important;border-width:0.5px !important; border-color:grey !important;" >
                        <f:selectItems value="#{decisionTreeBean.commentType}" var="X"
                            itemLabel="#{X}" itemValue="#{X}" />
                    </p:selectOneListbox>

jQuery

jQuery(".generateBtn").on("click",function(event){
                    name=PF('commmentWidget').selectItem($('.ui-selectonelistbox-item[data-label*="Other"]'));
                    console.log(name);

                });

这不起作用。另外,我的 widgetVar 上没有 val() 方法。

【问题讨论】:

  • 确定你的$('.ui-selectonelistbox-item[data-label*="Other"]') 选择器返回了正确的节点吗?像stackoverflow.com/questions/31866658/… 那样做选择节点的成本更低
  • 我在 selectonelistbox 项上没有看到任何 data-label 属性,甚至没有看到 .ui-selectonelistbox-item...
  • 遵循您共享的链接中提供的解决方案。当我尝试使用 PF('selectOneMenuWV').selectValue('3') --> 它给了我 selectValue is not a function 错误。在浏览器控制台中,我还可以看到 PF('selectOneMenuWV') 的原型中不存在此方法。我修改了我的元素以尝试按标签选择,但它也不起作用
  • 该组件确实无法选择值。我的意思是标签(第二个),如果没有正确的选择器,它就不起作用。你在哪里读到&lt;f:selectItems styleClass=".ui-selectOneListbox-item" value="#{decisionTreeBean.commentType}" var="X" itemLabel="#{X}" itemValue="#{X}" data-label="#{X}"/&gt; 的作品? 生成的 html 是什么样子的...基于此选择器。我认为你需要一个基于索引的 jquery 选择器并将结果项传递给 selectItem...
  • PF('commmentWidget').selectItem(PF('commmentWidget').jq.find("ul.ui-selectlistbox-list li:eq(4)"));这行得通。谢谢:-)

标签: primefaces


【解决方案1】:

PrimeFaces p:selectOneListbox 没有在选项/项目上设置任何属性,因此它们可以在“jquery”选择器中使用。所以列表中的“索引”最常用于该组件

PF('commmentWidget').selectItem(PF('commmentWidget').jq.find("ul.ui-selectlistbox-list li:eq(4)"));

在这里,我使用li:eq(4) 从列表中选择第 4 个元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    相关资源
    最近更新 更多