简体   繁体   English

禁用启用 <h:commandButton> 取消选择/选择 <h:selectBooleanCheckBox>

[英]Disable/Enable <h:commandButton> on deselecting/selecting <h:selectBooleanCheckBox>

I want to disable the submit button on my jsp Page if the user has not agreed to the terms and conditions. 如果用户未同意条款和条件,我想在我的jsp页面上禁用提交按钮。 I have tried many solutions available but still not getting through. 我尝试了很多可用的解决方案,但仍未通过。 Here is my code : 这是我的代码:

 <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>

Please help. 请帮忙。

May the following code snippet help you 可以使用以下代码段来帮助您

<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>

Also you could add required field. 您还可以添加必填字段。 Check this : RequiredCheckboxValidator 检查一下: RequiredCheckboxValidator

Your button is always enabled on the server side, so correcting user-controlled code on the client side will still execute business action associated with the command button. 您的按钮始终在服务器端启用,因此在客户端更正用户控制的代码仍将执行与命令按钮关联的业务操作。

What you want is to disable button on the server by setting its disabled attribute to the inverse of the current checkbox value: 你想要的是通过将其disabled属性设置为当前复选框值的倒数来禁用服务器上的按钮:

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

What remains to be done is to submit the form either via ajax, by nesting <f:ajax render="form:button" /> , or synchronously, by adding onclick="document.getElementById('form').submit();" 还有待做的是通过ajax,嵌套<f:ajax render="form:button" />或同步,通过添加onclick="document.getElementById('form').submit();" . Both changes must be done to the <h:selectBooleanCheckbox> tag. 必须对<h:selectBooleanCheckbox>标记进行两项更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM