0

问题:使用 aCActiveForm dropDownList我试图让它在更改时发布选定的值。但我不确定如何引用当前选择的下拉值?

到目前为止的代码是:

$form->dropDownList($model,'condition',$model->getConditionOptions(), array('submit'=>array('theme/build','id'=>$model->id,'condition'=>'')));

getConditionOptions返回下拉值数组的位置,“条件”应该是从此下拉列表中选择的值。

4

2 回答 2

1

不要仅仅为了具有当前值而指定属性——这违背了下拉菜单的目的。

要获取下拉列表的当前值,请在您的 jquery 中使用 jQuery 的 val: http://api.jquery.com/val/,例如:

$("#DropDownID").val()

编辑:您真正的问题似乎是如何在更改下拉列表后更新内容,具体取决于下拉列表的值。这将通过 AJAX 完成。Yii 为此提供了一个特殊的AJAX数组,例如:

$form->dropDownList($model,
                    'condition',
                    $model->getConditionOptions(),
                    array('submit'=>array('theme/build',
                          'id'=>$model->id,
                          'ajax'=>array('url'=>CController::createURL('controller/action'),
                                        'data'=>'$("#' . $model->id . '")',                                   
                                        'update'=>'#otherID', //selector to update
                                        )
                                    )
                          )
                    );

然后,在您的控制器端,您可以使用getParam获取值。请注意,您要更新的任何内容都应回显,而不是使用渲染。

编辑 2:如果您不想使用 jQuery,而只是重新加载整个页面,您应该只提交表单,例如:

$form->dropDownList($model, 'condition', $model->getConditionOptions(), array('submit'=>CController::createURL('controller/action')))

然后在您的控制器中,您仍然会使用 getParam 并呈现一个新页面。

于 2013-01-14T18:43:53.680 回答
0

我认为你可以这样做的方式是这样的:

//define your dropdownlist
$form->dropDownList(
    $model,
    'condition',
    $model->getConditionOptions(),
    //your ajax call
    'ajax'  => array( 
        'type'  => 'POST',
        'url'   => 'theme/build'.
        'data'  => array(
            'id' => $model->id,
            'condition' =>'js:jQuery(this).val()', //current value
        )
);

编辑:要获得当前选定的选项:

$('#dropbox').val();

或者

$('#dropbox :selected').val();

获取当前选定的选项文本

$('#dropbox :selected').text();
于 2013-01-14T18:40:54.380 回答