Yii2中的Ajax,异步更新表单
php页面的代码:
<?php
$script= <<< JS
// here you write all your javascript stuff
$('#zipCode').change(function(){
var zipId =$(this). val();
$.get ('index.php?r=locations/get-city-province',{ zipId:zipId },function(result){
var data =$. parseJSON(result );
$ ("#customers-city"). attr('value',data .city);
$ ("#customers-province"). attr('value',data .province);
});
});
JS;
$this ->registerJs( $script);
?>
控制器的代码:
use yii\helpers \Json;
class LocationsController extends Controller
{
public function actionGetCityProvince ($zipId){
//find the zipcode from the locations table
$location =Locations:: findOne($zipId );
echo Json::encode ($location);
}
}
https://www.zaozl.com 找资料
来自于datou:https://github.com/datou-leo/ci