CakePHP中的国家/州下拉列表 [英] Country/State Dropdown in CakePHP
问题描述
我该如何使用表单助手处理视图中的依赖组合框。例如:
How do i deal with dependent combo boxes in the views with the form helper. For example:
国家选择框(选择一个国家应筛选出所选国家的州)
国家选择框
Country Select Box (Choosing a country shall filter out the states of the selected country) States Select Box
这应该在Javascript / Jquery等帮助下发生。我遇到了一个与Cake的核心AJAX帮助器相同的示例,但是如果有人可以提供Java脚本示例的帮助,那就太好了。
This should happen with the help of Javascript / Jquery etc. I came across an example for the same with Cake's core AJAX helper but it would be very nice if someone can help with a Javascript example.
谢谢
推荐答案
在views / edit.ctp
In views/ edit.ctp
<script type="text/javascript">
$(document).ready(function (){
$('#country').change(function() {
$('#state').load('/controller/getStates/'+$(this).val());
});
});
</script>
<select id="country" name="country">
<option value="1">Greece</option>
</select>
<span id="state">
<select name="state">
<option value=""></option>
</select>
</span>
并在controller.php中
and in controller.php
function getStates(int countryID){
$this->set('selectbox',
$this->State->find('list',array('conditions'=>'State.Country_id='.$countryID,
'fields;=>array('description')));
}
和views / getStates.ctp
and views/getStates.ctp
<select name="state">
<option value=""></option>
<?php
foreach($selectbox as $option)
echo '<option value="'.$option['id'].'">'.$option['description'].'</option>'."\n";
?>
</select>
我希望我不要忘记一些东西
I hope I don't forget something
这篇关于CakePHP中的国家/州下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!