2015-02-11 48 views
0

使用Yii2我有下面的代碼顯示與我的類別列表下拉列表。是否可以使用下拉onchange使用值作爲參數重定向?

我想知道是否有可能,以便更改執行的操作是localhost:8888/article/category?id = 1而不是它是什麼atm(localhost:8888/article/category)。

這是否需要AJAX和/或JS或者只是使用PHP做什麼?(ID喜歡PHP)

<?php $form = ActiveForm::begin(['id' => 'category-form', 'action' => 
'/advanced/article/category',]); ?> 

<?= $form->field($model, 'category')->dropDownList($model->categoryList,[ 

'prompt'=>'Select Category to view', 
'onchange'=>'this.form.submit()' 

]) ?> 

<?php ActiveForm::end(); ?> 

回答

1

你不需要阿賈克斯這一點。 js會做到這一點。

<?= $form->field($model, 'category')->dropDownList($model->categoryList,[ 
           'prompt'=>'Select Category to view', 
           'id'=>'cat-id', 
          ]) ?> 

和JS選擇時

$this->registerJs( 
'$(document).ready(function(){ 
$("#cat-id").change(function(){ 
var e = document.getElementById("cai-id"); 
    var strSel = e.options[e.selectedIndex].value; 
    window.location.href="'.Yii::$app->urlManager->createUrl('category?id=').'" + strSel; 
}); 

});', View::POS_READY); 
2

如果你想使用onchange屬性,而無需編寫任何額外的JavaScript重定向,你需要改正你的代碼多一點得到它的工作。

1)更改您形成methodget(默認情況下它是post):

<?php $form = ActiveForm::begin([ 
    'id' => 'category-form', 
    'action' => '/advanced/article/category', 
    'method' => 'get', // Add this to your code 
]); ?> 

2)類別字段的精確設置名稱dropDownList選項,以避免formName()包裝:

<?= $form->field($model, 'category')->dropDownList($model->categoryList, [ 
    'prompt'=>'Select Category to view', 
    'onchange'=>'this.form.submit()', 
    'name' => 'category', // Add this to your code 
]) ?> 
-1

對不起,你可以做一個onchange的例子,但這次與一個放射學家yii2

字段($模型, 'estadoLaboral') - >單選列表(陣列( 'T'=> 'Actualmente特拉瓦霍褐', 'B'=> '雅沒有特拉瓦霍褐'))使用javascript

> ?
相關問題