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 yiihelpers Json; class LocationsController extends Controller
{
public function actionGetCityProvince ($zipId){ //find the zipcode from the locations table
$location =Locations:: findOne($zipId );
echo Json::encode ($location);
} }
$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 yiihelpers Json; class LocationsController extends Controller
{
public function actionGetCityProvince ($zipId){ //find the zipcode from the locations table
$location =Locations:: findOne($zipId );
echo Json::encode ($location);
} }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: Yii 2.0进阶版 高级组件 优化京东平台
- 下一篇: java怎么把2个字符串拼接在一起?