我是CakePHP和一般编码的初学者。我正在尝试创建一对动态下拉菜单,其中第二个选项取决于第一个选择的内容。我遵循了一些指南,但我似乎没有得到好的结果。最近我跟着这里的问题的答案:CakePHP re-populate list box但我得到的是一个"ReferenceError: $ is not defined"
当使用谷歌浏览器。CakePHP中的动态下拉菜单在Chrome中引发[ReferenceError]
我使用CakePHP 2.5.4与实例中所给出
我的用户控制数据库:
<?php
function beforeFilter()//executed before any controller action logic
{
$this->Security->enabled = false;
}
App::uses('AppController', 'Controller');
/**
* Users Controller
*
* @property User $User
* @property PaginatorComponent $Paginator
* @property SessionComponent $Session
*/
class UsersController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'Session');
/**
* index method
*
* @return void
*/
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
$countries = $this->User->Country->find('list');
$cities = $this->User->City->find('list');
$this->set(compact('countries', 'cities'));
// populate selects with options
$this->set('countries', $this->User->Country->find('list'));
$this->set('cities', $this->User->City->find('list'));
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
$countries = $this->User->Country->find('list');
$cities = $this->User->City->find('list');
$this->set(compact('countries', 'cities'));
}
/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->request->allowMethod('post', 'delete');
if ($this->User->delete()) {
$this->Session->setFlash(__('The user has been deleted.'));
} else {
$this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
}
查看/用户/ add.ctp
<?php
echo $this->Form->create('User');
echo $this->Form->input('country_id');
echo $this->Form->input('city_id');
echo $this->Form->input('name');
echo $this->Form->end('Submit');
$this->Js->get('#UserCountryId')->event('change',
$this->Js->request(
array('controller' => 'countries', 'action' => 'get_cities'),
array(
'update' => '#UserCityId',
'async' => true,
'method' => 'post',
'type' => 'json',
'dataExpression' => true,
'evalScripts' => true,
'data' => $this->Js->serializeForm(array('isForm' => true, 'inline' => true)),
)
)
);
国家控制器
<?php
App::uses('AppController', 'Controller');
/**
* Countries Controller
*
* @property Country $Country
* @property PaginatorComponent $Paginator
* @property SessionComponent $Session
*/
class CountriesController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'Session');
public function get_cities(){
Configure::write('debug', 1);
$cities = array();
if(isset($this->request->query['data']['User']['country_id'])){
$cities = $this->Country->City->find('list', array(
'conditions' => array('City.country_id' => $this->request->query['data']['User']['country_id'])
));
}
$this->set('cities', $cities);
}
/**
* index method
*
* @return void
*/
public function index() {
$this->Country->recursive = 0;
$this->set('countries', $this->Paginator->paginate());
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->Country->exists($id)) {
throw new NotFoundException(__('Invalid country'));
}
$options = array('conditions' => array('Country.' . $this->Country->primaryKey => $id));
$this->set('country', $this->Country->find('first', $options));
}
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
$this->Country->create();
if ($this->Country->save($this->request->data)) {
$this->Session->setFlash(__('The country has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The country could not be saved. Please, try again.'));
}
}
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
if (!$this->Country->exists($id)) {
throw new NotFoundException(__('Invalid country'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Country->save($this->request->data)) {
$this->Session->setFlash(__('The country has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The country could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Country.' . $this->Country->primaryKey => $id));
$this->request->data = $this->Country->find('first', $options);
}
}
/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
$this->Country->id = $id;
if (!$this->Country->exists()) {
throw new NotFoundException(__('Invalid country'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Country->delete()) {
$this->Session->setFlash(__('The country has been deleted.'));
} else {
$this->Session->setFlash(__('The country could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
}
查看/城市/ get_cities.ctp
<?php
if(!empty($cities)){
foreach ($cities as $id => $name) {
?>
<option value="<?php echo $id; ?>"><?php echo $name; ?></option>
<?php
}
}
?>
你确定你添加了jQuery库例如在你的布局? – marian0 2014-10-07 20:27:58
@ marian0对不起,我是一个完整的noob,你能为我详细说明吗? :) – Farstucker 2014-10-10 20:58:50