如何在 Yii 中使用 ajax 更改下拉列表中的值

how to change values in a dropdown list using ajax in Yii

在视图页面中我有 2 个下拉列表

<?php
$list = CHtml::listData($category, 
                'cid', 'cname');    
echo $form->dropDownList($model,'cid',$list,
              array('empty' => '(Select a category)')); 
?> 
<label>Sub Category *</label>
<?php  
$subcategory = array();
echo $form->dropDownList($model,'sid',$subcategory,
              array('empty' => '(Select a subcategory)')); 
?> 

使用类别中的值我必须更改子类别值

这是我的 ajax 函数

<script type="text/javascript">
    $(function (){ 
        $("#Product_cid").change(function (){             
            var cid = $('#Product_cid').val();
            var path = "<?php echo $this->createUrl('admin/mysubcategory') ?> ";
            $.ajax({  
            type: "POST",      
            url: path, //url to be called
            data: { cid: cid } //data to be send 
            }).done(function( msg ) {
                 alert(msg)
                 $("#Product_sid").val("msg");
                 $("#Product_sid").selectmenu('refresh'); 
.
             });

        });
    });
</script>

这是我的控制器

public function actionMysubcategory()
{
        $cid = Yii::app()->request->getPost('cid'); 
        $subcategory= Subcategory::model()->findAll(
                array('order'=>'sid',
               'condition'=>'cid=:cid', 
               'params'=>array(':cid'=>$cid)));
            $list = CHtml::listData($subcategory, 
            'sid', 'sname');    

        print_r($list);
} 

我从控制器获取列表。如何制作成下拉菜单???

在你的控制器中你可以

public function actionMysubcategory(){
    $model = new YourModel();
    $cid = Yii::app()->request->getPost('cid'); 
    $subcategory= Subcategory::model()->findAll(
            array('order'=>'sid',
           'condition'=>'cid=:cid', 
           'params'=>array(':cid'=>$cid)));
        $list = CHtml::listData($subcategory, 
        'sid', 'sname');
       echo CHtml::activeDropDownList($model,'sid',$list);
    } 

在你看来你可以这样做

<?php
       $list = CHtml::listData($category, 
            'cid', 'cname');    
       echo $form->dropDownList($model,'cid',$list,
             array('empty' => '(Select a category)')); 
?> 
<label>Sub Category *</label>

 <div id="sub-category-wrapper">
 <?php  
      $subcategory = array();
      echo $form->dropDownList($model,'sid',$subcategory,
          array('empty' => '(Select a subcategory)')); 
  ?>
</div>

然后在你的 ajax

<script type="text/javascript">
$(function (){ 
    $("#Product_cid").change(function (){             
        var cid = $('#Product_cid').val();
        var path = "<?php echo $this->createUrl('admin/mysubcategory') ?> ";
        $.ajax({  
        type: "POST",      
        url: path, //url to be called
        data: { cid: cid }, //data to be send
        success: function( response ) {
             $('#sub-category-wrapper').html(response);
         }
        })

     });
 });

可能是 jQuery slectmenu ui 的问题。检查 ajax return 值在哪里是正确的。

然后尝试

$('#Product_sid').val(msg).selectmenu('refresh',true);

静态版本到 select 'ttttt' 选项

//16 is representing 'ttttttt' in your option stack.
$('#Product_sid').val(16).selectmenu('refresh',true);