神奇的checkbox选择
触发前:
触发后:
html代码
<tr>
<td>
<p class="short-input ue-clear">
<label>所在地:</label>
<select name="provinceid">
<?php echo Ousuclass::all_html_option($pros, "id"); ?>
</select>
<select name="cityid">
<?php if (isset($cities)): ?>
<?php echo Ousuclass::all_html_option($cities, "id"); ?>
<?php else: ?>
<option value="">请先选择所在市</option>
<?php endif; ?>
</select>
</p>
</td>
</tr>
<tr>
<td>
<table id="usercities" style="border:none;width:800px; margin-left: 120px;">
<?php if (isset($counties)): ?>
<tr>
<?php $i = 0; foreach ($counties as $city): ?>
<?php if (3 == $i): ?>
</tr>
<tr>
<?php $i = 0; endif; ?>
<td>
<p class="short-input ue-clear">
<label><?php echo $city["name"]; ?>:</label>
<input type="checkbox" name="<?php echo "city_" . $city["id"]; ?>"/>
</p>
</td>
<?php $i++;endforeach; ?>
</tr>
<?php endif; ?>
</table>
</td>
</tr>
jquery代码
<script type="text/javascript">
function change() {
$("select[name=provinceid]").change(function () {
$("select[name=countyid]").empty();
$("select[name=countyid]").append("<option value=''>请先选择所在地</option>");
var pid = $(this).val();
var url = "<?php echo "/".$url_module."/".$url_model?>/change/" + pid;
if ("" == pid)
return;
$.ajax({
type: "Post",
url: url,
data: {"id": pid},
success: function (res) {
var r = $.parseJSON(res);
if (r.ret == 0) {
var select = $("select[name=cityid]");
select.empty();
select.append("<option value=''></option>");
for (var i in r.obj) {
select.append("<option value='" + r.obj[i].id + "'>" + r.obj[i].name + "</option>");
}
} else {
alert(r.msg);
}
}
});
});
$("select[name=cityid]").change(function () {
var pid = $(this).val();
var url = "<?php echo "/".$url_module."/".$url_model?>/change/" + pid;
if ("" == pid)
return;
$.ajax({
type: "Post",
url: url,
data: {"id": pid},
success: function (res) {
var r = $.parseJSON(res);
if (r.ret == 0) {
var select = $("select[name=countyid]");
select.empty();
select.append("<option value=''></option>");
$("#usercities").empty();
var k = 0;
var ht = "<tr>"
for (var i in r.obj) {
if (3 == k) {
ht += "</tr><tr>";
k = 0;
}
ht += "<td><p class=\"short-input ue-clear\"><label>" + r.obj[i].name + ":</label><input type='checkbox' name='city_" + r.obj[i].id + "'/></p></td>";
select.append("<option value='" + r.obj[i].id + "'>" + r.obj[i].name + "</option>");
k++;
}
ht += "</tr>";
$("#usercities").append(ht);
} else {
alert(r.msg);
}
}
});
});
}
</script>
php代码
<?php
public function _getGlobalData($data) {
$data = parent::_getGlobalData ( $data );
//获取省份数据的信息
$result = $this->mydb->find("select administrativedivisioncode as id,administrativedivisionname as name from dictxzqdms where level = 1");
$data["pros"] = $result["obj"];
if ($this->input->server('REQUEST_METHOD') == 'GET'&&"edit"==$this->url_method){
if(isset($data['datalist'])){
if(isset($data['datalist']['provinceid'])){
$result = $this->mydb->find("select administrativedivisioncode as id,administrativedivisionname as name from dictxzqdms where parentcode='".$data['datalist']['provinceid']."'");
$data["cities"] = $result["obj"];
}
if(isset($data['datalist']['cityid'])){
$result = $this->mydb->find("select administrativedivisioncode as id,administrativedivisionname as name from dictxzqdms where parentcode='".$data['datalist']['cityid']."'");
$data["counties"] = $result["obj"];
}
}
}
public function change($proid=""){
if(""==$proid){
$result["ret"] = 1;
$result["msg"] = "no id";
echo json_encode($result);
exit;
}
$sql = "select administrativedivisioncode as id,administrativedivisionname as name from dictxzqdms where parentcode=$proid";
$re = $this->mydb->find($sql);
$result["ret"] = 0;
$result["obj"] = $re["obj"];
echo json_encode($result);
}
?>