2016-04-24 15 views
0

在這張圖片中,我想選擇類別,然後我想在另一個下拉菜單中選擇子類別。在類別中選擇值我想調用從數據庫中獲取子類別的Ajax,並將其放入另一個子類別下拉菜單中。這個怎麼做?什麼時候從下拉列表中選擇一個分類,然後我想在其他分類下的子類別?

image

+0

的可能的複製[檢索下拉列表mysql數據庫和插入數據庫在jsp](http://stackoverflow.com/questions/6168836/retrieve-dropdown-list-from-mysql-database-and-insert-to-database-in-jsp) –

+0

標題仍然是無法解讀,但也許其他人會有時間來解決它。 – peterh

回答

0

因爲這是客戶端,你需要通過服務器從數據庫中獲取的數據。你可以閱讀this link瞭解更多詳情。

0

在視圖頁面中調用哪裏是想改變CHAGE類

$(document).on('change', '#cat_id', function() { 
     var cat_id = $(this).val(); 
     $.ajax({ 
     type: 'POST', 
     url: '<?php echo site_url('controller_name/subCatByCatId'); ?>', // here call your function where you want to send cat_id 
     data: {cat_id: cat_id}, 
     success: function (data) { 
      $('#sub_cat_id').html(data); 
     } 
     }); 
    }); 

子類別中的AJAX功能,這是你的服務器端功能

function subCatByCatId() { 
    $cat_id = $_POST['cat_id']; 
    $query = $this->db->query("select * from sub_category where cat_id=$cat_id")->result();//here replace your query 
    $returnVal = '<option value = "">Select one</option>'; 
    if (!empty($query)) { 
     foreach ($query as $row) { 
      $returnVal .= '<option value = "' . $row->sub_cat_id . '">' . $row->sub_cat_name . '</option>'; 
     } 
    } 
    echo $returnVal; 
    } 
+0

我正在使用spring MVC。 – javed

+0

以及它沒關係,如果你能理解它,希望你可以在你的項目中實現 –

+0

你剛纔在這裏'url:'<?php echo site_url('controller_name/subCatByCatId'); '>','打電話給你的控制器並且功能 –

相關問題