Sometimes you want to get selected value of radioButtonList using jQuery in your Yii powered application and it feels difficult due to Yii’s field naming conventions, as every field name contains array. Lets take an example. Suppose you have a radioButtonList as shown below and you want to enable/disable another field based on user selection from radioButtonList. We have added a JavaScript function onChange event of our radio buttons. Below code demonstrate how you can check selected value of radioButtonList using jQuery and perform conditional logic. <div class="row"> <?php echo $form->labelEx($model, 'nationality'); ?> <?php echo $form->radioButtonList($model, 'nationality', array( 'US' => 'US', 'Non US' => 'Non US', ), array( 'onChange'=>'toggleSSN();' )); ?> <?php echo $form->error($model, 'nationality'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'ssn'); ?> <?php echo $form->textField($model, 'ssn', array('size' => 9, 'maxlength' => 9, 'disabled'=>true)); ?> <?php echo $form->error($model, 'ssn'); ?> </div> function toggleSSN(){ var nationality = $('input[name="User[nationality]"]:checked').val(); if(nationality == 'US') { $('#User_ssn').attr('disabled', false); } else { $('#User_ssn').attr('disabled', true); } }
JavaScript
Get selected value of radioButtonList using jQuery in Yii
Sometimes you want to get selected value of radioButtonList using jQuery in your Yii powered application and it feels difficult due to Yii’s field naming
· 5 min read
Related posts
JavaScript
Toggle checkboxes with jQuery and Prototype JS
Sometimes you work with forms having a long list of checkboxes, in such scenarios a “Check All” or “Toggle Selection” button comes handy
JavaScript
JQuery Multi fileupload
File Upload widget with multiple file selection, drag&drop support, progress bars and preview images for jQuery. Supports cross-domain, chunked and resumabl
JavaScript
JQuery tooltip
Here is a nice jQuery tool tip. How to use: 1- Download the file 1.1- Rename the file tipsy-zip.doc to tipsy.zip 1.2- Extract the zip folder or download it from