2014-04-22 3 views
5

In Yii 1 ho potuto impostare onchange evento per DropDownList con questo metodo:Yii2 DropDownList onchange

CHtml::dropDownList('id', $select, $list, array('onchange'=>'this.form.submit()')) 

Ed era abbastanza buono. Ma in Yii 2 non funziona, come posso risolvere questo problema?

+0

[questo] (http://stackoverflow.com/questions/36500793/how-to-create-a-dropdown-dependent-on-another-dropdown -in-yii2/36643901 # 36643901) può aiutare qualcuno. –

risposta

10

questo codice sottopone sul cambiamento correttamente:

<?php use yii\helpers\Html; ?> 
    <?= Html::beginForm() ?> 
    <?= Html::dropDownList(
    'test', //name 
    'b', //select 
    ['a'=>'A', 'b'=>'B'], //items 
    ['onchange'=>'this.form.submit()'] //options 
)?> 
    <?= Html::endForm() ?> 
+0

hm, hai ragione, forse nella versione precedente non funzionava – Alex

+0

Questo funziona a meno che tu non debba inserire del testo tra virgolette. Le virgolette si convertono in caratteri speciali. Come garantire che le virgolette non vengano convertite? Questo JS inserito non funzionerà: onchange = "if ($ (" #name ") .val() == ' john ') {page.reload();}. – lubosdz

+0

è possibile utilizzare virgolette o simboli di escape diversi: oncambiare => "if ($ ('# nome'). val() == 'john') {page.reload();} oppure puoi usare JsExpression come qui: http://stackoverflow.com/questions/23260636/ aggiornare-nascosto-campo-con-completamento automatico-in-yii2/23.261.772 23.261.772 # – user1852788