到目前为止,我还没有找到关于SO的明确答案,所以如果已经回答,请尽可能指导我。
我有什么:
(HTML)
<span class="btn-group" data-toggle="buttons-radio">
<button id="btn1" onClick="$('#theTest').val('price');" type="button" class="btn" value="price">Price</button>
<button id="btn2" onClick="$('#theTest').val('time');" type="button" class="btn" value="time">Time</button>
<input name="theTest" id="theTest" type="hidden" value=""/>
</span>
单击按钮时,我需要使用单击按钮的值调用辅助bean中的函数,即#theTest
的值
我该怎么做?我无法通过javascript调用它,因为我使用的是JSF,#{}
标记不适用于javascript。
答案 0 :(得分:1)
只需使用生成所需HTML而不是纯HTML元素的JSF组件。
E.g。
<h:outputStylesheet library="bootstrap" name="css/bootstrap.min.css" target="head" />
<h:outputScript library="bootstrap" name="js/bootstrap.min.js" target="head" />
...
<h:form>
<span class="btn-group" data-toggle="buttons-radio">
<h:commandButton type="button" styleClass="btn" value="Price"><f:ajax listener="#{bean.change('Price')}" /></h:commandButton>
<h:commandButton type="button" styleClass="btn" value="Time"><f:ajax listener="#{bean.change('Time')}" /></h:commandButton>
</span>
</h:form>
使用
public void change(String value) {
System.out.println(value);
}
使用JSF,您甚至可以创建生成所需HTML的自定义组件,以便最终得到更像这样的光滑代码:
<b:radioButtons listener="#{bean.change}">
<b:radioButton value="Price" />
<b:radioButton value="Time" />
</b:radioButtons>