【问题标题】:how to change values in a dropdown list using ajax in Yii如何在 Yii 中使用 ajax 更改下拉列表中的值
【发布时间】:2015-02-16 07:03:01
【问题描述】:

在视图页面中我有 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);
} 

我从控制器获取列表。怎么可以做成下拉菜单???

【问题讨论】:

  • 响应的内容和效果如何? alert(msg)...

标签: jquery ajax post drop-down-menu yii


【解决方案1】:

在你的控制器中你可以

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);
         }
        })

     });
 });

【讨论】:

    【解决方案2】:

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

    然后试试

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

    选择“ttttt”选项的静态版本

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      • 2011-09-20
      • 2021-01-09
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      相关资源
      最近更新 更多