Javascript CakePHP中的动态下拉菜单在Chrome中抛出[ReferenceError]

Javascript CakePHP中的动态下拉菜单在Chrome中抛出[ReferenceError],javascript,php,jquery,cakephp,Javascript,Php,Jquery,Cakephp,我是CakePHP和一般编码的初学者。我试图创建一对动态下拉菜单,其中第二个菜单中的选择取决于第一个菜单中选择的内容。我已经遵循了一些指南,但我似乎没有得到好的结果。最近我在这里找到了问题的答案:但我得到的只是一个ReferenceError:$在使用谷歌浏览器时没有定义 我将CakePHP2.5.4与示例中给出的数据库一起使用 我的用户控制器: <?php function beforeFilter()//executed before any controller action lo

我是CakePHP和一般编码的初学者。我试图创建一对动态下拉菜单,其中第二个菜单中的选择取决于第一个菜单中选择的内容。我已经遵循了一些指南,但我似乎没有得到好的结果。最近我在这里找到了问题的答案:但我得到的只是一个ReferenceError:$在使用谷歌浏览器时没有定义

我将CakePHP2.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'));
    }
}
View/users/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库。您可以通过两种方式完成此操作:下载一个文件并将其放入webroot/js目录或从链接

如果选择第一个解决方案,则必须访问并下载库的当前版本。然后将文件移动到APP/webroot/js路径下的项目中。最好将文件名更改为jquery.js,而不提供版本信息。最后一件事是打开布局文件,该文件应该位于APP/View/Layouts/your_layout_file.ctp中。如果您没有自定义布局,它将被称为default.ctp。最后一步是查找标记并放置以下行:

<?php echo $this->Html->script('jquery'); ?>
或者,若不更改名称,则必须将文件的完整名称作为param传递给该文件,而不带扩展名

如果您想要从外部源(如Google CDN)获取链接库,只需从站点获取jQuery的url,并在布局文件中放入以下行:

<?php echo $this->Html->script('http://code.jquery.com/jquery.min.js'); ?>

有关HtmlHelper和脚本方法的更多信息,请阅读。

您确定在布局中添加了jquery库吗?@marian0对不起,我是一个彻头彻尾的noob,您能为我详细说明一下吗?: