我做了这个依赖的下拉列表,对于第一个下拉列表,它已经从mysql数据库加载了,但是对于第二个下拉列表,它无法被第一个值加载。
查看
<div class="form-group">
<label class="sr-only" for="form-email">Country</label>
<select name="country" id="country" class="form-control" onChange="get_state(this.value, 'ship');">
<option value="" selected="selected">Pilih Voucher</option>
<?php
if ($results) {
foreach ($results as $row) {
echo "<option value=\"" . $row -> country_id . "\">" . $row -> country_name . "</option>";
}
}
?>
</select>
</div>
<div class="form-group">
<label class="sr-only" for="form-email">State Option</label>
<select name="state_ship" id="state_ship" class="form-control">
<option value="" selected="selected">State Option</option>
<?php
if ($result_state) {
$str = 1;
foreach ($result_state as $row) {
while($str <= $row -> country ){
echo "<option value=\"" . $str . "\">" . $str . "</option>";
$str++;
}
}
}
?>
</select>
</div>
我的控制器
public function ship()
{
$data=array(
'results'=> $this->produk_model->getAllData('produk'),
);
$this->load->view('transaksi/header');
$this->load->view('transaksi/ship', $data);
$this->load->view('transaksi/footer');
public function get_state($country)
{
$this->db->where('country_id', $country_id);
$result_state = $this->db->get('country_name');
}
我的模特
public function getAllData($table)
{
return $this->db->get($table)->result();
}
JQUERY
function get_state(value, type)
{
$('#state_' + type).html('Loading State...');
$.ajax({
dataType : "json",
type : "POST",
url : "produk/get_state",
data : "data=" + value + "&type=" + type,
success : function(res){
$('#state_' + type).html('');
if(res.status == 200)
{
for(var i = 0; i < res.data.length; i++)
{
$('#state_' + type).append('<option value="' + res.data[i].id_country + '">' + res.data[i].nama_country + '</option>');
}
}
else
{
$('#state_' + type).append('<option> Data Unloaded </option>');
}
},
error : function(){
},
timeout: 50000
});
}
}
感谢您的帮助
答案 0 :(得分:0)
<%= f.select :start, Location.all.collect {|x [x.city_name, x.id]}, {:include_blank => 'Select One'} %>
并在你的观点
success: function(data)
{
$('#state_ship').html('<option value="">-- Select State--</option>');
$.each(data, function(i, data){
$('#state_ship').append("<option value='"+data.state_id+"'>"+data.state_name+"</option>");
});
}