views:

9561

answers:

5

Hello, I am having some difficulty accessing the value of the selected radio button in a radiogroup. I've attempted a number of different approaches based upon discussion in other posts on the forum and around the web. Unfortunately haven't been lucky (or skilled) enough to get it working. Based on the following FormPanel config I was hoping someone could show me how to get the value of the selected radio in the group 'mainPhone'.

Thanks!

Wanted to update to indicate that I was able to get a response from stackoverflow while the EXT-JS forums didn't provide me with any help. Way to go stackoverflow!

Matt

function createForm(elem) {
var myForm2 = new Ext.form.FormPanel({
  renderTo:elem,
  width:425,
  frame:true,
  style:"margin: 10px auto 10px auto;",
  items: [{xtype:'fieldset',
   title: 'Contact Info',
   autoHeight:true,
   items :[new Ext.form.RadioGroup({
      fieldLabel: 'Main Phone',
      vertical: false,
      id:"mainPhone",
      items: [
       {boxLabel: 'Home', name: 'id-1', inputValue: 'H', checked:true},
       {boxLabel: 'Work', name: 'id-1', inputValue: 'W'},
       {boxLabel: 'Other', name: 'id-1', inputValue: 'O'}
      ]    

     }),
      new Ext.form.TextField({
     id:"frm_CreateCustomerHomePhone",
     fieldLabel:"Home Phone",
     width:275,
     allowBlank:true
     }),
     new Ext.form.TextField({
     id:"frm_CreateCustomerWorkPhone",
     fieldLabel:"Work Phone",
     width:275,
     allowBlank:true
     })
      new Ext.form.TextField({
     id:"frm_CreateCustomerOtherPhone",
     fieldLabel:"Other Phone",
     width:275,
     allowBlank:true
     })
 ]}]});    
}
A: 

This is something of a wild guess, but how about this:

myForm2.getForm().getValues()['id-1'];
Adam Bellaire
This one did it! Many thanks
Matty
A: 
function get_radio_value()
{
    for( var i=0; i < document.myForm.mainPhone.length; i++ )
    {
       if( document.myForm.mainPhone[ i ].checked )
       {
           return document.myForm.mainPhone[ i ].value;
       }
    }
}
Thanks Adam for taking the time to work that out. I'm going to attempt an implementation this morning and will update with my progress shortly.
Matty
Don't thank me, the answer came from live-fliptone: http://stackoverflow.com/users/51817/-live-fliptone ... I just edited the answer to format his code, in community wiki only the most recent editor shows up.
Adam Bellaire
+2  A: 

The method getValue() on the radio group itself will return the object that is checked, if any, otherwise, it returns undefined.

(by the way I set value instead of inputValue for my boxes, although I don't think it makes much of a difference, maybe it does on the last "getValue"), I'm using extjs 3.0, and my radiogroup configuration is slightly different than yours.

 var checkedItem = Ext.getCmp('mainPhone').getValue();

 if (checkedItem == undefined) return '';

 return checkedItem.getGroupValue();
 // The getGroupValue will return the value of the checked option in a group,
 // unfortunately, it only seems to work on the items and not the radiogroup 
 // itself
Cindy
A: 

if you want to get the specific value of the field, use

myForm2.getForm().findField('id-1').getGroupValue();
Joshua
A: 

I know this question is old, but I'm adding this for reference. The following snipit is valid for Ext 2.2 afaik.

Ext.getCmp("mainPhone").items.get(0).getGroupValue();