YII: How to update ajax data on page load?

I have a dropdown that updates the ajax field. Everything works fine, but the ajax field is not populated with page load data. How can I force update the ajax field when the page loads?

This is a drop down list:

echo CHtml::dropDownList('category_selection_code', '', $cat_list_data, 
        array('style'=>'width:400px',
              'ajax' => 
                        array(
                            'type'=>'POST', 
                            'url'=>CController::createUrl('articles/GetContentFields'), 
                            'update'=>'#ExtraContentFields', 

                        )
            )
);

* UPDATE

I found a solution that works for me:

$(document).ready(function(){
    $.ajax({
        type: 'POST',
        data: {category_selection_code: $('#category_selection_code').val()},
        url: '<?php echo CController::createUrl('articles/GetContentFields'); ?>',
        success: function(data){
                    $('#ExtraContentFields').html(data)
                }
    })
})

The controller for handling ajax articles / GetContentFields expects category_selection_code in the $ _POST array. And we need to install it in the data section of the ajax part:

: { category_selection_code : $ ('# category_selection_code'). val ()},


Thnx is all for reference.

+3
source share
3

, . -, CHtml:: dropdownList() HTML. . CHtml:: dropdownList()

AJAX

$(document).ready(function() {
 //Your call goes in here. 
});
+1

. , CHtml::activeId(..).

<?php
$cs = Yii::app()->clientScript->registerScript(
  'update-brand-car-on-page-load',
  '$(document).ready(function() {
        $("#'.CHtml::activeId($model, 't_brand_car').'").change();
    });'
);
?>
+1

Perhaps this will help:

jQuery(function($) {
    $('input name["category_selection_code"]').change();
});
0
source

All Articles