[SalesForce] How to disable a radio button on visualforce page

I am having a Radio button having values Yes, No. On selecting Yes I need to disable another radio button present on the same page. How this can be done. Please help ???

Best Answer

You can use apex:actionsupport to do this the SF Way:

A simple VF Page:

<apex:page controller="TestController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:selectRadio label="Should the next question be disabled?" value="{!disabled}">
                    <apex:selectOptions value="{!yesNo}"/>
                    <apex:actionSupport event="onclick" status="stat" action="{!test}" reRender="test"/>
                </apex:selectRadio>
                <apex:outputPanel id="test">
                    <apex:selectRadio value="{!country}" disabled="{!isDisabled}">
                        <apex:selectOptions value="{!items}"/>
                    </apex:selectRadio>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
     </apex:form>
</apex:page>

And the controller:

public with sharing class TestController {
    public String country{get; set;}
    public String city{get; set;}
    public String disabled{get; set;}

    public Boolean getIsDisabled() {
        return 'Y'.equals(disabled);
    }
    public PageReference test() {
        return null;
    }

    public List<SelectOption> getYesNo() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('Y','Yes')); 
        options.add(new SelectOption('N','No')); 
        return options; 
    }

    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('US','US')); 
        options.add(new SelectOption('CANADA','Canada')); 
        options.add(new SelectOption('MEXICO','Mexico')); return options; 
    }
}
Related Topic