0
我有一個從選定狀態的城市的依賴選擇框。CakePHP在提交表單後選擇依賴框變空了
工作很好!但在提交表單後,城市選擇框變空。
我想知道如何在提交表單後填充選擇框。
我正在使用CakePHP 2.5。
我可以在表單提交後看到城市選擇框的值,我不知道如何在提交表單後再次填充。
if ($this->request->is('ajax'));
{
$this->set('stateCities', $this->request->data['stateCities']);
}
//controller
public function getCitiesByStateAjax()
{
$this->autoRender = false;
$this->autoLayout = false;
$city_id = $this->data['city'];
$this->loadModel('city');
$citiesList = $this->UsuarioSimulador->listCitys($city);
$citiesList = Hash::combine(
$citiesList, '{n}.city.id', array('%s', '{n}.city.name')
);
echo json_encode($citiesList);
}
//form
debug($estateCities);
echo $this->Form->input('City.city', array('label'=>'City',
'type' => 'select',
'empty'=>'- select one -',
'options' => $stateCities,
'data-webroot' => $this->webroot)
);
//Ajax javascript code :
$(function() {
$(document).on('change', "select#State", function()
{
var url = '<?php echo Router::url(array('controller' => 'cities', 'action' => 'getCitiesByStateAjax'));?>';
var data = {
State: $(this).val()
};
$.ajax({
url: url,
method: 'POST',
data: data,
dataType: 'json',
beforeSend: function(xhr) {
$.blockUI({
message: '<h2>Search</h2>'
});
},
complete: function() {
$.unblockUI();
},
success: function ($response, a, b)
{
if(b.status == 200) {
var $optionsCity = $("#Cities").html($("<option />").val('').text('- select -'));
if($response) {
$optionsCity.prop("disabled", false);
$.each($response, function(idestate, value) {
var $opt = new Option(value, idestate);
$optionsCity.append($opt);
});
} else {
$optionsCity.prop("disabled", true);
}
} else {
alert($response);
}
},
error: function(x, t, m) {
var msg = 'Ocorreu um erro inesperado. ';
if(t === "timeout") {
msg += "Aguarde 30 segundos e tente novamente!";
} else {
msg = t;
}
alert(msg);
}
});
});