簡體   English   中英

禁用啟用 <h:commandButton> 取消選擇/選擇 <h:selectBooleanCheckBox>

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

如果用戶未同意條款和條件,我想在我的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>

請幫忙。

可以使用以下代碼段來幫助您

<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

您的按鈕始終在服務器端啟用,因此在客戶端更正用戶控制的代碼仍將執行與命令按鈕關聯的業務操作。

你想要的是通過將其disabled屬性設置為當前復選框值的倒數來禁用服務器上的按鈕:

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

還有待做的是通過ajax,嵌套<f:ajax render="form:button" />或同步,通過添加onclick="document.getElementById('form').submit();" 必須對<h:selectBooleanCheckbox>標記進行兩項更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM