You need to sign in to do that
Don't have an account?
Get value from the selected selectOption in a selectList
Suppose I have a select list as shown in this visualforce code:
<apex:selectList id="type" size="1" value="{!type}" onchange="{!test}"> <apex:selectOption itemLabel="Type1" itemValue="Type1" /> <apex:selectOption itemLabel="Type2" itemValue="Type2" /> <apex:selectOption itemLabel="Type3" itemValue="Type3" /> </apex:selectList>
and I want to alert the value of the selected selectOption from the selectList as shown in this apex code:
public string selectedType; public string getType() { return selectedType; } public void setType(string value) { selectedType = value; } public string getTest() { return 'alert("'+selectedType+'");'; }
Now, I'm guessing the default getter and setter of the type value of the selectList does not work as I thought it would since this apex code simply alerts "null".
Does anyone know how to make this work or how to get the value of the selected selectOption within the selectList?
Try action region.
I have not tried it myself. BUt, I am aware that action region does something similar.
All Answers
http://www.salesforce.com/us/developer/docs/pages/index.htm
Checkout this link
Using this link you can get the value and then you can show the alert to user using java script.
Srini,
This link is the main page of the vf developer's guide instead of a specific page. I have done searches here and cannot find a sufficient example. The example for selectList uses a dataList to display the selected selectOption values. It does not explain nor show how one would use the selected selectOption value(s) in the corresponding controller. I'm having a hard time extrapolating what I need from the documentation.
Could you provide a direct link to something you think is useful/helpful? Or provide an example?
I'm going to continue searching on google and such.
Thanks!
Still not having any luck. I've tried using apex:actionSupport event="onchange" with rerender and action attributes. I've tried using apex: param inside the actionSupport and inside a commandButton. I just can't seem to get access to the value of the selected selectOption from my apex code.
Can anyone help me?????
Hey I am sorry,
In that VF Guide, check out the section Using JavaScript to Reference Components
It will help you out.
Try action region.
I have not tried it myself. BUt, I am aware that action region does something similar.
Here is an example using a radiobutton. First the visualforce:
<apex:pageBlockSection title="Ticket Tier">
<apex:selectRadio value="{!tickettier}">
<apex:selectOptions value="{!Tiers}"/>
</apex:selectRadio>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!setticket}" value="Finish"/>
</apex:pageBlockButtons>
At least for radio buttons I think you need to have an action command button to get the value into the variable.
The apex code to set the selectOptions is this:
String tickettier = null;
public List<SelectOption> getTiers() {
List<SelectOption> Tiers = new List<SelectOption>();
if (camp.t1_name__c <> null) {
Tiers.add(new SelectOption(camp.t1_name__c, camp.t1_name__c));
}
if (camp.t2_name__c <> null) {
Tiers.add(new SelectOption(camp.t2_name__c, camp.t2_name__c));
}
if (camp.t3_name__c <> null) {
Tiers.add(new SelectOption(camp.t3_name__c, camp.t3_name__c));
}
return Tiers;
}
public String gettickettier() { return tickettier; }
public void settickettier(String tickettier) { this.tickettier = tickettier; }
The Visualforce guide has good examples of how to get the page to refresh once you have the selectOption value.
aeben,
Thanks a lot! That does the trick.