1.advanced\backend\web目录下新建js文件夹用来放js文件
//myjs.js
$(function(){
$('.link').change(function(){
getSalutationValue(this.value);
});
function getSalutationValue(value) {
if(value == 1){
$("#abc").html('ok');
// $("#abc").hide();
}
if(value == 2){
$("#abc").html('nopass');
// $("#abc").show();
}
if(value == 'Unspecified'){
$("#abc").html('nothing');
}
}
});
2.修改advanced\backend\assets\AppAsset.php,添加自定义的js文件的路径,我这就是js/myjs.js
<?php
namespace backend\assets;
use yii\web\AssetBundle;
/**
* Main backend application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
'js/myjs.js'
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
3.打开要引入js的views里面的某个页面,在title定义后加入AppAsset::register($this);
4. 下拉列表需要定义一个名字,用来被js控制
<?= $form->field($model,'status')->dropDownList([1=>'ok',2=>'nopass'],['class'=>'form-control link','prompt'=>'choose'
]);?>
这里是form-control link
整个视图页面代码如下:
<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \common\models\LoginForm */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use backend\assets\AppAsset;
$this->title = 'Login';
$this->params['breadcrumbs'][] = $this->title;
AppAsset::register($this);
?>
<div class="site-login">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to login:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model,'status')->dropDownList([1=>'ok',2=>'nopass'],['class'=>'form-control link','prompt'=>'choose'
]);?>
<div id="abc">1q2w3e4r</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>