【问题标题】:How to get <p:selectOneMenu> value using JQuery如何使用 JQuery 获取 <p:selectOneMenu> 值
【发布时间】:2023-03-17 08:05:01
【问题描述】:

如何使用 Jquery 或 javascript 获取选择组件值

MyJSF 代码如下

<h:form>
      <p:selectOneMenu style="width:150px" id="id" onchange="onCall()">
              <f:selectItem itemLabel="Select" itemValue=""></f:selectItem>
              <f:selectItem itemLabel="Other" itemValue="Other"></f:selectItem>
       </p:selectOneMenu>
       <p:selectBooleanCheckbox id="flag"/>
       <p:inputText id="name" value="#{mybean.value}/>
 </h:form>

请帮我解决这个问题

【问题讨论】:

  • 我尝试了一些代码,但没有一个工作
  • 如果你可以发布你渲染的 html,而不是 jsf 代码,如果你可以发布你的 javascript,就像你提到的那样,你已经应用了一些内联 js 事件。

标签: jquery jsf primefaces


【解决方案1】:

由于 Primefaces 为其组件提供了 Javascript API,您可以通过 widgetVar 访问它

xhtml

<p:selectOneMenu widgetVar="selectWV">               
</p:selectOneMenu>

JS

PF('selectWV').getSelectedValue();//gets the value
PF('selectWV').getSelectedLabel();//gets the label

【讨论】:

  • 是否有任何选项可以将 设置为 true 并隐藏
    当然可以,但是这个问题与上面提出的问题无关,你可以问另一个问题来解释你的情况,在这种情况下我们避免混淆......
【解决方案2】:

你可以在这个函数中传递this

onchange="onCall(this)"

我假设你已经在全局范围内定义了这个 onCall 函数,那么你可以这样做:

onCall(el){
    alert(el.value);
}

【讨论】:

  • 为什么要使用 alert 而不是 console.log() 进行调试?
  • 这只是 OP 必须寻找答案的一个例子。我知道console.log() 更好用,但这只是我想说的假设告诉 OP 尝试这样做以获取函数中的值。
  • 哦,工作,但我也想设置 checkbox=true 值
猜你喜欢
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
相关资源
最近更新 更多