设置“disabled = true”时<h:commandbutton>的动作未被调用</h:commandbutton>

时间:2013-11-19 13:09:23

标签: jsf action commandbutton

我希望在选中T&amp; C复选框时启用“提交”按钮。虽然我的书面逻辑工作正常但是h:commandButton的操作即使在检查复选框后启用它也没有被调用。最初该按钮被禁用。如果我删除disabled =“TRUE”属性但代码不起作用,代码工作正常。这是我的代码:

<script type="text/javascript">
$(document).ready(start);
function start() {

    $('.testing').click(
            function() {
                if ($(document.getElementById('form2:check'))
                        .is(':checked')) {
                    $(document.getElementById('form2:saveForm'))
                            .removeAttr('disabled');
                } else {
                    $(document.getElementById('form2:saveForm')).attr(
                            'disabled', 'disabled');
                }
            });
                 }
</script>

JSP代码:

<h:form id="form2">
    <div class="testing">
        <h:selectBooleanCheckbox id="check" />
                Agree           
            <li class="buttons ">
                <div class="center">
                    <h:commandButton id="saveForm" styleClass="btTxt submit"
                        type="submit" value="Submit"
                        action="#{declarationFormBean.formSubmit}" disabled="true"></h:commandButton>
                </div>
            </li>
</h:form>

请帮忙。

1 个答案:

答案 0 :(得分:2)

那是因为您只使用JavaScript / jQuery删除了JSF生成的HTML表示中的disabled属性。这不会改变实际JSF组件中的disabled属性,在解码动作事件期间会查询该属性。

这实际上是JSF的Good Thing TM ,因为否则任何黑客都可以绕过任何JSF disabledreadonlyrendered属性只是操纵HTML DOM树,而它们最初用于阻止它们,就像rendered="#{request.isUserInRole('ADMIN')}“。

一样

您需要通过JSF而不是JS / jQuery启用按钮。

<h:selectBooleanCheckbox binding="#{check}">
    <f:ajax render="saveForm" />
</h:selectBooleanCheckbox>
<h:commandButton id="saveForm" ... disabled="#{not check.value}" />

这就是全部。不需要自定义JS / jQuery混乱,也不需要任何其他JSF bean属性。上面的代码按原样完成。

另见:

相关问题