请帮助一个游手好闲者。我试图填充依赖于第一个的下拉列表。第一个是国家,当它被改变时,城市应该被填充。我已经在网上跟踪了各种教程(一些来自于放射流程)并且卡住了。我对ajax和jquery知之甚少。最重要的是,我是codeigniter的新手。以下是我的代码
州的模型
public function get_list(){
$this->db->from($this->table);
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[$row['sid']] = $row['state'];
}
}
return $return;
}
城市模型
public function get_cities($state = null){
$this->db->select('ctid, city');
if($state != NULL){
$this->db->where('state_id', $state);
}
$query = $this->db->get($this->table);
$cities = array();
if($query->result()){
foreach ($query->result() as $cit) {
$cities[$cit->ctid] = $cit->city;
}
return $cities;
}else{
return FALSE;
}
}
我的控制器
public function index()
{
$data['state_option'] = $this->State_model->get_list();
$this->load->view('_parts/header');
$this->load->view('index', $data);
$this->load->view('_parts/footer');
}
public function dependent_dropdown($state)
{
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->city_model->get_cities($state)));
}
我的jquery / ajax
$(document).ready(function(){
$('#state').change(function(){
// $("#city").html("<option>Loading...</option>");
$('#city > option').remove();
var state = $('#state').val();
if (state != "") {
$.ajax({
type: "POST",
data:'state',
url: "<?php echo site_url('site/dependent_dropdown'); ?>"+state,
success: function(data){
$.each(data, function(i, data){
$('#city').append("<option value='"+data.city+"'>"+data.city+"</option>");
});
}
});
}
else{
$('#city').html('<option>--Select City--</option>');
}
});
});
我的观点(州下拉列表)
<select id="state" name="state" class="form-control" required>
<option value="" selected>--Select State--</option>
<?php foreach ($state_option as $state) { ?>
<option value="<?php echo $state ?>"><?php echo $state; ?></option>
<?php } ?>
</select>
(城市下拉列表)
<select id="city" name="city" class="form-control" required>
<option value="" selected>--Select City--</option>
</select>
答案 0 :(得分:1)
我最终解决了我的问题。我改变了视图以显示状态数据
$(document).ready(function(){
$('#state').change(function(){
$('#city').html('<option>Loading....</option>');
var state = $('#state').val();
if (state != "") {
$.get("<?php echo base_url(); ?>site/dependent_dropdown/"+state).success(function(data){
$('#city > option').remove();
data = JSON.parse(data);
for(var i in data){
$('#city').append("<option value='"+i+"'>"+data[i]+"</option>");
}
})
}
else{
$('#city').html('<option>--Select City--</option>');
}
});
});
然后我改变了我的ajax调用
template<class It>
struct range_t {
private:
It b, e;
public:
It begin() const { return b; }
It end() const { return e; }
decltype(auto) front() const { return *b; }
decltype(auto) back() const { return *std::prev(e); }
bool empty() const { return b==e; }
range_t without_front( std::size_t n = 1 ) const {
auto r = *this;
std::advance(r.b,n);
return r;
}
range_t without_back( std::size_t n = 1 ) const {
auto r = *this;
std::advance(r.e,std::ptrdiff_t(-n));
return r;
}
range_t(It s, It f):b(std::move(s)), e(std::move(f)) {}
range_t():b(), e() {}
};
template<class It>
range_t<It> range( It b, It e ) {
return {std::move(b), std::move(e)};
}
答案 1 :(得分:0)
检查您的Ajax请求,更改
url: "<?php echo site_url('site/dependent_dropdown'); ?>"+state,
到
url: "<?php echo base_url('site/dependent_dropdown'); ?>/"+state,