20,359
社区成员
发帖
与我相关
我的任务
分享
<?php
/**
* User: xinxinj
* Date: 15/8/13
* Time: 下午9:35
*/
// 分类
include "UnlimitedClassIfication.php";
$object = new UnlimitedClassIfication();
$a = $object->getArea();
?>
<html>
<title>三级联动</title>
<body>
<select class="cate" data-flag="1" id="f1">
<option value="0">请选择</option>
<?php foreach ($a as $k => $v) :?>
<option value="<?php echo $k; ?>"><?php echo $v; ?></option>
<?php endforeach; ?>
</select>
<select class="cate" data-flag="2" id="f2">
<option value="0">请选择</option>
</select>
<select class="cate" data-flag="3" id="f3">
<option value="0">请选择</option>
</select>
</body>
<script src="jq.js"></script>
<script type="text/javascript">
$(".cate").change(function(){
var type = $(this).val();
var id = $(this).attr("data-flag");
if (id != 3) {
if (id == 1) {
$("#f"+id).next().next().html("");
}
$.ajax({
url:"http://test.com/UnlimitedClassIfication.php",
type:"get",
data:{"flag":1, "type":type},
async:false,
success:function(a) {
var a = eval("("+ a + ")");
// alert(a.status);
if(a.status == 0) {
alert(a.mess);
} else if (a.status == 1) {
$("#f"+id).next().html(a.data);
}
}
});
}
});
</script>
</html>
UnlimitedClassIfication.php
<?php
/**
* User: xinxinj
* Date: 15/8/13
* Time: 下午9:35
*/
/**
* 三级联动
*/
class UnlimitedClassIfication
{
/**
* 地区信息.
*
* @return array.
*/
function area ()
{
return array(
1 => array(
2 => "北京",
3 => "山西"
),
2 => array(
4 =>"北京",
),
3 => array(
5 => "太原",
6 => "晋中"
),
4 => array(
7 => "朝阳",
8 => "海淀"
),
5 => array(
9 => "清徐",
10 => "小店",
),
6 => array(
11 => "介休",
12 => "太谷"
)
);
}
/**
* 获取一级分类.
*
* @return return
*/
function getArea ()
{
$area = $this->area();
return $area[1];
}
/**
* 三级联动.
*
* @param int $type 分类标识.
*
* @return string
*/
function getUnlimitedClassIfication ($type)
{
header("Content-type: text/html; charset=utf-8");
$area = $this->area();
if (isset($area[$type])) {
$content = "<option value=0>请选择</option>";
foreach ($area[$type] as $k => $v) {
$content .= "<option value='{$k}'>{$v}</option>";
}
$return = array("status" => 1, "mess" => "", "data" => $content);;
} else {
$return = array("status" => 0, "mess" => "该分类不存在子分类", "data" => array());
}
echo json_encode($return);
}
}
if (isset($_GET['flag'])) {
$type = $_GET['type'];
$object = new UnlimitedClassIfication();
$object->getUnlimitedClassIfication($type);
}