[初学web]phalcon框架下通过ajax更改select表单的选项
2015-11-16 20:53
92 查看
最后效果如下:
在更改city选项时候,触发自动更改District的选项
代码如下:
//AddressController.php
/**
* Edits a addres
*
* @param string $uid
*/
public function editAction($uid)
{
if (!$this->request->isPost()) {
$addres = Address::findFirstByuid($uid);
if (!$addres) {
$this->flash->error("addres was not found");
return $this->dispatcher->forward(array(
"controller" => "address",
"action" => "index"
));
}
$this->view->CITY = AddressDef::$CITY;
$this->view->DISTRICT = AddressDef::$DISTRICT[$addres->city];
$this->view->uid = $addres->uid;
//设定好默认值
$this->tag->setDefault("city", $addres->city);
$this->tag->setDefault("district", $addres->district);
$this->tag->setDefault("zone", $addres->zone);
$this->tag->setDefault("building", $addres->building);
$this->tag->setDefault("unit", $addres->unit);
$this->tag->setDefault("room", $addres->room);
$this->tag->setDefault("remark", $addres->remark);
}
}
[/codesyntax]
[codesyntax lang="php"]
/*address/edit.phtml*/
<table>
<tr>
<td align="right">
<label for="city">City</label>
</td>
<td align="left">
<?php echo $this->tag->selectStatic(array("city", $CITY, "onchange"=>"loadXMLDoc()"));?>
</td>
</tr>
<tr>
<td align="right">
<label for="district">District</label>
</td>
<td align="left">
<?php echo $this->tag->selectStatic(array("district", $DISTRICT));?>
</td>
</tr>
<tr>
<td align="right">
<label for="zone">Zone</label>
</td>
<td align="left">
<?php echo $this->tag->textField(array("zone", "type" => "number")) ?>
</td>
</tr>
<tr>
<td align="right">
<label for="building">Building</label>
</td>
<td align="left">
<?php echo $this->tag->textField(array("building", "type" => "number")) ?>
</td>
</tr>
<tr>
<td align="right">
<label for="unit">Unit</label>
</td>
<td align="left">
<?php echo $this->tag->textField(array("unit", "type" => "number")) ?>
</td>
</tr>
<tr>
//.............
[/code]
phaclon中input控件可以这么写
<?php echo $this->tag->textField(array("zone", "type" => "number")) ?>
上面的语句呈现出的html语句就是
<input type="text" id="zone" name="zone" value="3" />
其中value="3"是在controller中
$this->tag->setDefault("zone", $addres->zone);
来控制的
select控件可以这么写
<?php echo $this->tag->selectStatic(array("city", $CITY, "onchange"=>"loadXMLDoc()"));?>
其中的$CITY是在controller中这里定义的
$this->view->CITY = AddressDef::$CITY;
onchange事件,触发执行loadXMLDoc()
<script> function loadXMLDoc() { var xmlhttp; //通过id名city获取到控件对象 var city=document.getElementById("city"); //获取到当前选择的值 var cityvalue=city.options[city.selectedIndex].value; console.log(cityvalue); if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { var districts=xmlhttp.responseText; //获取到名为district的select的组件 var district=document.getElementById("district"); //先清空 district.length=0; //将从后端返回的json字符串转为javascript数组 districts=eval("("+districts+")"); console.log(districts); //遍历数组 $.each(districts,function(key,value){ console.log("key:"+key+";value:"+value); //添加select的options选项 district.options.add(new Option(value,key)); }); } } console.log("/address/getDistrict/"+cityvalue); xmlhttp.open("GET","/address/getDistrict/"+cityvalue, true); xmlhttp.send(); } </script>
对应在controller中后端接口如下
public function getDistrictAction($city){
//这句很重要,不然返回的就不止json的字符串,还有外围的很多html
$this->view->setRenderLevel(0);
if (!$this->request->isGet()) {
$this->flash->error("invalid action!");
}
$feed = AddressDef::$DISTRICT[$city];
//JSON_UNESCAPED_UNICODE为了汉字能正确编码解析
echo json_encode($feed,JSON_UNESCAPED_UNICODE);
}
[/code]
转载于:https://my.oschina.net/u/2244980/blog/531101
相关文章推荐
- 通过Ajax方式绑定select选项数据
- 通过Ajax方式绑定select选项数据的实例
- 根据select标签中option选项的变化动态更改表单元素
- Android WebView加载HTML表单并通过javascript提交
- 【接上文】Ajax处理响应,连接 Web 表单
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- select下拉框通过ajax获取后台的值
- javaweb之request通过各种表单提交项收集用户数据和用request获取表单提交数据
- 第三百三十四节,web爬虫讲解2—Scrapy框架爬虫—Scrapy爬取百度新闻,爬取Ajax动态生成的信息
- 玩转web之json(五)---将表单通过serialize()方法获取的值转成json
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- Web层框架的下一个王者,如果不是Ajax,将是业界的悲哀
- jquery validation 在触发按钮单击事件进行表单校验和验证通过后发起ajax请求的解决方案
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
- Ajax中通过JS代码自动获取表单元素值的示例代码
- 使用Yii框架自带的CActiveForm实现ajax提交表单
- liferay中使用jQuery Validation Engine进行ajax验证,不通过也提交表单的解决方案
- ci框架如何重新填充set_select() set_checkbox() set_radio()表单
- 表单用serialize()序列化并且通过ajax向后台传值,后台方法参数传递问题