【发布时间】:2015-05-28 23:35:00
【问题描述】:
这是我的查询。我正在为许可协议页面设计 UI(在 JSF 中),其中“下一步”按钮将根据用户接受或拒绝许可协议的选择启用或禁用。接受和拒绝按钮是单选按钮。我为此使用jquery。这是jquery sn-p:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
$('#pg2\\:accept').click(function() {
alert("hello");
if ($(this).is(':checked'))
$('#next').removeAttr('disabled');
else
$('#next').attr('disabled', 'disabled');
});
}
</script>
这是我的 JSF 代码:
<h:panelgroup id="pg2">
<h:selectOneRadio id="radios" layout="pageDirection">
<f:selectItem id="accept" itemValue="#{true}" itemLabel=ACCEPT/>
<f:selectItem id="decline" itemValue="#{false}" itemLabel=DECLINE/>
</h:selectOneRadio>
</h:panelGroup>
<h:commandButton id="next" action="welcome" value="Next" widgetVar="nxt" />
编辑:用 Jsf 组件替换 primefaces 组件,因为我不能在我的代码中使用 primefaces
【问题讨论】:
-
<p:commandButton>有自己的disabled属性,您可以根据条件设置该属性。不需要摆弄额外的 JavaScript/jQuery 代码。 (在这种情况下,id可能已被添加,除非您将<h:form>的prependId设置为false,这反过来又很讨厌,应该始终保持不变(默认为true)。它是不仅仅是next。查看呈现的HTML将有助于获得与按钮对应的正确id。 -
此外,PrimeFaces 是一个基于 jQuery 的 JSF 组件库。手动包含 jQuery 是完全没有必要的,只会导致冲突。
-
我将用 jsf 组件替换 primeface 组件,因为我本质上需要 jquery。我最近知道我不能在我的代码中使用 primefaces。
-
这仍然适用于
<h:commandButton>s。<h:commandButton>还具有disabled属性,该属性需要一个布尔值,您可以有条件地设置该值。如果您仅在客户端使用 JavaScript/jQuery 禁用组件,则其服务器端状态仍将保持活动状态并且可被利用。