我想从城市列表中获取用户选择的城市商店的地址。我用ajax来显示城市。
$('.cn').change(function(){
$('.st1 option').remove();
$('.st1').append("<option value=''><Select city></option>");
var curr=$(this).val();
$.get('includes/pages/getcity.php',{cnid:curr},function(resp){
var obj = jQuery.parseJSON(resp);
$.each(obj, function() {
$data="<option value='"+this.id +"'>"+ this.city +"</option>";
$('.st1').append($data);
});
});
});
State <select name="state_id" class="select cn state_textbox" >
<option>---Select State--</option>
<?php
$query="select * from state";
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result)){
if($row['state_id']==0){
echo "<option value='".$row['id']."'>".$row['state']."</option>";
}
}
?>
</select> <br><br><br>
City<select class="st1 state_textbox" name="st1" style="margin-left:35px" >
<option selected="selected">-- Select your city --</option>
</select>
答案 0 :(得分:0)
在选择框中添加一个事件来处理所选项目:
<select name="state_id" class="select cn state_textbox" onchange="get_address(this.value);" >
在你的jquery脚本中:
function get_address(state){
var post_url = "destination file to process the form";
$.ajax({
type: "POST",
url: post_url,
datatype: "json",
success: function(data)
{
$('#address').val(data.address);
}
});
}
});
当然,您需要有一个运行查询的函数来读取正确的地址并将其返回给此函数! 也许是这样的:(PHP代码)
function get_address($state){
$q = "select address from tablename where state=?";
$query_result=$this->db->query($q,$em_code);
$address;
if($query_resul->result()){
foreach ($query_resull->result() as $output) {
$address = $output->address;
}
return $address;
} else {
return FALSE;
}
}
答案 1 :(得分:0)
你错过了申报变量......
试试这个
$.each(obj, function(i,v) {
var $data="<option value='"+v.id +"'>"+ v.city +"</option>";
//^^^----here;
$('.st1').append($data);
});