How to disable another checkbox field checkbox if i check one checkbox in checkbox field

Sample Code:

Visualforce page:


<apex:page controller="sample">

<script type="text/javascript">

</script>
  
    <apex:form >
  
    <apex:pageMessages />
  
    <apex:pageBlock >
        <apex:pageBlockSection columns="2">
            <apex:pageblockSectionItem >
                <apex:outputLabel value="Metro"/>
            </apex:pageblockSectionItem>      
            <apex:pageblockSectionItem >              
                <apex:inputCheckbox value="{!metro}">           
                    <apex:actionSupport event="onchange" reRender="a" action="{!demo}"/>
                </apex:inputCheckbox>
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:outputLabel value="City"/>
            </apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:inputCheckbox value="{!city}" id="a" disabled="{!bool}"/>
            </apex:pageblockSectionItem>          
        </apex:pageBlockSection>      
    </apex:pageBlock>

    </apex:form>

</apex:page>


Apex Code:

public class sample
{  
    public Boolean metro {get;set;}
    public Boolean city {get;set;}
    public Boolean bool {get;set;}
  
    public sample()
    {
      
    }
  
    public void demo()
    {
        if(metro)
        {
            bool = true;
        }
        else
        {
            bool = false;
        }       
    }
    
}


Cheers!!!

1 comment:

  1. Hi, how if i want to make the user check only one at a time, i tried replacing the disabled="{!bool}" with selected="{!bool}" but, doesn't seem to work, appreciate your response.

    ReplyDelete