在取消选择/选择<h:selectbooleancheckbox> </h:selectbooleancheckbox> </h:commandbutton>时禁用/启用<h:commandbutton>

时间:2013-11-19 06:26:16

标签: jsf commandbutton

如果用户未同意条款和条件,我想在我的jsp页面上禁用提交按钮。我尝试了很多可用的解决方案,但仍未通过。这是我的代码:

 <h:form id="form2" styleClass="align topLabel page">
 <h:selectBooleanCheckbox id="check" onclick="document.getElementById('form2:saveForm').disable = !this.checked"/>Agree                 
                <li class="buttons ">
                    <div class="center">
                        <h:commandButton id="saveForm" styleClass="btTxt submit"
                            type="submit" value="Submit"
                            action="#{declarationFormBean.formSubmit }"></h:commandButton>
                    </div>
                </li>
</h:form>

请帮忙。

2 个答案:

答案 0 :(得分:2)

以下代码段可以帮助您

<script type="text/javascript">
        function checkClick(check) {
            document.getElementById('form2:saveForm').disabled = !check.checked; }
</script>

<h:form id="form2" >
        <h:selectBooleanCheckbox id="check" onclick="checkClick(this)"/>Agree                 
        <li class="buttons ">
            <div class="center">
                <h:commandButton id="saveForm" styleClass="btTxt submit"
                                 type="submit" value="Submit" disabled="true" ></h:commandButton>
            </div>
        </li>
</h:form>

您还可以添加必填字段。检查一下:RequiredCheckboxValidator

答案 1 :(得分:2)

您的按钮始终在服务器端启用,因此在客户端更正用户控制的代码仍将执行与命令按钮关联的业务操作。

您想要的是通过将其disabled属性设置为当前复选框值的倒数来禁用服务器上的按钮:

<h:selectBooleanCheckbox binding="#{agree}" ... />
<h:commandButton disabled="#{not agree.value}" ... />

还有待完成的工作是通过ajax,嵌套<f:ajax render="form:button" />或同步,通过添加onclick="document.getElementById('form').submit();"来提交表单。必须对<h:selectBooleanCheckbox>标记进行两项更改。