简体   繁体   中英

How to disable apex command button based on a checkbox being checked or unchecked?

I'm creating a visualforce page for my company and I want the submit button to be disabled unless a checkbox on the page is changed. I was able to do this with a regular input button but I need this to be done with an Apex command button so I can have them update the record which will fire a back-end process.

Here is my code

<apex:page standardController="RMA__c">
<apex:form >
    <apex:outputPanel id="thePanel" rendered="{!(RMA__c.RLI_has_QUOTE_SO__c == true)}">
    <apex:pageBlock id="TheBlock" title="Confirm Information" mode="edit">
        <apex:pageBlockSection id="theSection" title="Confirm the  new field values" columns="2" rendered="{!(RMA__c.Show_the_box__c == true)}">
            <apex:outputfield value="{!RMA__c.Contact__c}"/>
            <apex:outputfield value="{!RMA__c.Shipping_Priority__c}"/>
            <apex:outputfield value="{!RMA__c.Ship_to_Address__c}"/>
            <apex:outputfield value="{!RMA__c.Bill_to_Address__c}"/>
            <apex:inputfield value="{!RMA__c.Request_Priority__c}"/>
            <apex:inputField id="reviewed" value="{!RMA__c.Changes_are_reviewed__c}"/> 
        </apex:pageBlockSection>
        <apex:pageblockButtons id="button">
        <apex:commandButton value="Submit" action="{!save}" id="saveit" disabled="true"/>
        </apex:pageblockButtons>
        <script>
         var checker = document.getElementById("{!$Component.TheBlock.theSection.reviewed}");
         var sendbtn = document.getElementById("{!$Component.TheBlock.button.saveit}");
         checker.onchange = function()
         {
          if(this.checked)
          {
            sendbtn.disabled = false;
          } 
          else 
          {
            sendbtn.disabled = true;
          }
         }
       </script>
   </apex:pageBlock>
    </apex:outputPanel>
</apex:form>

You can use reRender property in RMA__c.Changes_are_reviewed_c inputfield

First, wrap command button with <apex:outputpanel>

<apex:outputpanel>
  <apex:commandButton value="Submit" action="{!save}" id="saveit" disabled="{!RMA__c.Changes_are_reviewed__c==false}"/>
</apex:outputpanel>

then, rerender it from

<apex:inputField id="reviewed" value="{!RMA__c.Changes_are_reviewed__c}" reRender="saveit"/>

Hope, it will help. I have not compiled the code in salesforce, there may have some compilation error. But, this way, it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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