我有两个下拉列表,一个用于国家/地区,另一个用于我页面上的城市。当我最初加载页面时,我将国家/地区设置为从用户注册加载的默认国家/地区。然后我加载那个国家的城市。
问题是现在当我更改国家下拉列表并刷新页面时,我的国家下拉列表选择的值保持不变,但城市正在从默认国家加载。
我从控制器的视图中获得了正确的值,但视图没有正确设置它。我认为的代码如下
echo $this->Form->input('from_country_code',
array(
'options'=>$countries,
'id'=>'from_country_code',
'label' => __('Country',true),
'selected'=>$selectedCountryCode
)
);
其次,请解释我如何检测页面已刷新并保留更改的下拉值并且不执行整个动作控制器代码。
任何帮助将不胜感激。
更新——这是我的控制器代码
function add() {
$currentUser = $this->Auth->user();
$countryMap = $this->requestAction('/countries/getList/');
$this->set('countries',$countryMap);
$this->set('selectedCountryCode',$currentUser['City']['countriesCode']);
$cityMap = $this->requestAction('/cities/getListByCountryCode/'.$currentUser['City']['countriesCode']);
$this->set('cities',$cityMap);
if(!empty($this->data)) {
if($this->Request->saveAll($this->data)) {
$this->Session->setFlash('The Request was successfully Posted');
$this->redirect(array('controller'=>'users','action'=>'requests'));
} else {
$this->Session->setFlash('The Request was not saved. Please try again');
}
}
}
请注意,我遇到的问题是在页面刷新时国家下拉列表的选定属性设置不正确。即使我刷新页面,我也检查了控制器的值是否正确。
我在此变量@selectedCountryCode 中设置选定的国家/地区值。