本文介绍了如何使用Ajax在Cakephp中根据国家选择状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如何使用Ajax在Cakephp中根据国家/地区选择状态? 我想根据所选国家/地区添加州名单。例如,如果选择india ,那么印度国家将出现在州下拉等等。它不起作用。当我要根据国家选择州时。 请帮助我。下面是我的编码 控制器中的 App :: uses(' AppController', ' Controller'); class VenuesController扩展AppController { public $ uses = array(' 状态',' Country',' Venue',' Facility',' 用户',' 图像'); public $ components = array(' Custom ); ################## add state ########################## #### 公共职能州() { if($ this-> RequestHandler-> isAjax()){ $ this-> layout = ' ajax'; $ countryid = $ this-> request-> data(' country_id') ; $ selectstate = $ this-> State-> find(' list',array(' fields' => array(' state_name'),' conditions' => array(' State.country_id' => $ countryid))); $ this-> set(' selectbox',$ selectstate); // pr($ selectstate); } } } addvenue.ctp中的 < script src = <?php echo HTTP_ROOT;?> js / jquery.min。 1.8.3.js type = text / javascript > < ; / script > < script type = text / javascript > function showFields(id) { alert(id); // alert(你好); dataString =country_id =+ id; $ .ajax({ url:场地/州,类型:POST,数据:dataString, //警报(数据); 成功:函数(数据) { $('#state')。html(数据); },}); } < / script > < section class = content-header > < h1 > 添加Venue < / h1 > < ol class = breadcrumb > < li > ; < a href = http:// localhost / locatevenue / users > < i class = fa fa-dashboard > < / i > 主页< / a > < / li > < li class = 有效 > 添加Venue < / li > < / ol > < / 部分 > <! - 主要内容 - > < 部分 class = content > < div class = row > <! - 左栏 - > <? php echo $ this - > 会话 - > flash('success_message')?> < div class = col-md-61 > <! - 一般表单元素 - > < div class = box box-primary > < div class = box-header > < h3 class = box-title > 添加Venue < / h3 > < / div > < div style = width:49%; float:left; > <? php echo $ this - > Form-> create('Venue',array('id'=>'myform','name'=>'myform','enctype' =>'multipart / form-data'));?> < div class = box-body > < div class = 表格 - 小组 > <? php echo $ this - > 表单 - >输入('country',数组('onChange'=>'showFields(this.value)','class'=>'form-control' ,'id'=>'country','type'=> 'select','label'=> true,'label'=>'country:','options'=> $ country,'empty'=> '选择国家','必需'=>'假')); ?> < / div > < div class = form-group > <? php echo $ this - > Form-> input('state',array('class'=>'form-control','id'=>'state','type'=> 'select','label'=> true,'label'=>'state:','options'=> $ state,'empty'=> '选择一个州','必需'=>'假')); ?> < / div > <! - < div id = 州>< / div> - > < / div > < / div > <! - /.box-body - > < / div > < / div > < / div > < / section > 解决方案 使用 = array(' State',' 国家',' Venue',' 设施',' User',' Image'); public components = array(' Custom'); ################## add state ########################## #### public function state() { if( this-> RequestHandler-> isAjax()){ How Can I Select State According To Country In Cakephp Using Ajax?I want to add state list according to the selected country. For e.g if indiais selected then india States appears in states drop down etc.Its not working.when i am going to select state according to country.please help me.below is my codingin controllerApp::uses('AppController', 'Controller');class VenuesController extends AppController {public $uses = array('State','Country','Venue','Facility','User','Image');public $components = array('Custom');##################add state############################## public function state() {if($this->RequestHandler->isAjax()) {$this->layout = 'ajax'; $countryid=$this->request->data('country_id'); $selectstate = $this->State->find('list', array('fields' => array('state_name'), 'conditions' => array('State.country_id' => $countryid))); $this->set('selectbox',$selectstate); //pr($selectstate);} }}in addvenue.ctp<script src="<?php echo HTTP_ROOT;?>js/jquery.min.1.8.3.js" type="text/javascript"></script><script type="text/javascript">function showFields(id){alert(id);//alert("hello");dataString="country_id="+id; $.ajax({ url: "venues/state", type: "POST", data: dataString, //alert(data); success: function(data) { $('#state').html(data); }, });}</script><section class="content-header"><h1> Add Venue </h1><ol class="breadcrumb"> <li><a href="http://localhost/locatevenue/users"><i class="fa fa-dashboard"></i> Home</a></li> <li class="active">Add Venue</li></ol></ section><!-- Main content --><section class="content"> <div class="row"> <!-- left column --> <?php echo $this->Session->flash('success_message') ?> <div class="col-md-61"> <!-- general form elements --> <div class="box box-primary"> <div class="box-header"> <h3 class="box-title">Add Venue</h3> </div> <div style="width:49%; float:left;"> <?php echo $this->Form->create('Venue' ,array('id' =>'myform' ,'name' => 'myform','enctype'=>'multipart/form-data')); ?> <div class="box-body"> <div class="form-group"> <?php echo $this->Form->input('country', array('onChange'=>'showFields(this.value)','class'=>'form-control','id' =>'country','type' => 'select','label'=>true,'label'=>'country:','options' => $country ,'empty' => 'Select A Country','required'=>'false')); ?> </div> <div class="form-group"> <?php echo $this->Form->input('state', array('class'=>'form-control','id' =>'state','type' => 'select','label'=>true,'label'=>'state:','options' => $state ,'empty' => 'Select A State','required'=>'false')); ?> </div> <!--<div id="state"></div>--> </div> </div> <!-- /.box-body --> </div> </div> </div></section> 解决方案 uses = array('State','Country','Venue','Facility','User','Image');public components = array('Custom');##################add state############################## public function state() {if(this->RequestHandler->isAjax()) { 这篇关于如何使用Ajax在Cakephp中根据国家选择状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 03:56