I have apex:selectlist on my vf page which is populated by a selectoptionList.
In my Vf page i also use jquery to dynamically add a new option. But this does not pass the selected option to the controller. The newly added option is not passed.
On searching for the cause i came upon another question on the boards
http://forums.sforce.com/t5/forums/forumtopicprintpage/board-id/Visualforce/message-id/31836/print-single-message/false/page/1
which mentions that SF does a validation if the selected value is part of the original selectoption and if it isnt then doesnt pass the value.
To work around this
I thought i could call an actionfunction and use jquery to get the selected value of the select and pass this value as parameter and assign to a controller variable. But this also doesnt seem to work.
VF Page
// Adding the new option
if (!selects.find('option[value="' + accid + '"]').length) {
alert(accid);
selects.append('<option value="' + accid + '">Attach to Existing : ' + accname + '</option>');
}
selects.change(function(){ // this captures the change event
alert('selected value is ' + $(this).val());
var selAccount = $(this).val();
alert(selAccount); // correct values are displayed on the alerts
setaccsel(selAccount);
<apex:actionFunction action="{!setselectedAcc}" name="setaccsel" rerender="">
<apex:param name="Acc" assignTo="{!selectedAccounta}" value=""/>
</apex:actionFunction>
<apex:pageBlockSectionItem id="pbsiAccountName">
<apex:outputlabel value="Account Name" id="lbAccountName"/>
<apex:outputPanel id="optableAccount" style="position:relative" layout="block">
<apex:inputfield value="{!ct.AccountId}" id="accountLookup" />
<apex:selectList id="selectedAccountId" value="{!selectedAccount}" size="1" styleClass="accSelectandlookup" style="width:250px" >
<apex:selectOptions value="{!AccountList}"/>
</apex:selectList>
</apex:outputPanel>
</apex:pageBlockSectionItem>
controller
public PageReference setselectedAcc()
{
system.debug('setselectedAcc called');
system.debug('Selected Accounta....BBBBBBB' + selectedAccounta);// This still shows the value of the first value in the selectoption list
system.debug('Selected Account is on top' + selectedAccount);
return null;
}
Am i doing something wrong here?
Best Answer
I have had the same problem. The solution was to use tha native html select list and an visualforce hidden field. After each change on the selectlist the currently selected value is saved to the hidden field:
You can pre fill the html select list with options from the database:
Controller:
Page: