2017-10-16 85 views
-2

我希望根據上一個選擇中的選擇更改下拉選擇。所以我有一個數據庫來存儲車型和車型的值。結構是id,make,model 目前,我有一個函數來獲取make,然後獲取模型,然後調用Make選項和Model選擇的函數,但它顯示了所有這些,我不希望。 那麼我該如何做到這一點? 我已經在這裏搜索過,並看到了東西,但我仍然困惑,沒有經驗這樣做。根據之前的選擇自動下拉更改

不要真的以爲你需要我目前的任何代碼,但如果你這樣做,請讓我知道。

+0

您需要使用'ajax'和'jquery'或'javascript'的值。檢查此[鏈接](https://stackoverflow.com/questions/11237900/first-drop-down-menu-to-auto-change-the-options-of-a-second-dropdown) –

+0

好的,我會採取非常感謝。 –

+0

可能重複的[選擇下拉動態使用JavaScript時,頁面重新加載,並將改變另一個下拉等](https://stackoverflow.com/questions/45705095/selected-drop-down-dynamically-using-javascript-when-頁面重載和意志變化) – Gagantous

回答

0

我不是那麼肯定你的意思,但根據我的理解,

你有兩個下拉選擇, 一個爲父選擇,第二個是孩子選擇, 孩子選擇下拉值取決於父母選擇, 是你的意思?

如果是,

一個簡單的方法來做到這一點,是使用jQuery和AJAX,

你必須閱讀基本Ajax和事件偵聽器要做到這一點,

1選擇下拉,

<select id="parent_select"> 
</select> 

<select id="child_select"> 
</select> 

/*這將填充你的父母選擇*/

$.ajax({ 
    url: 'url that will display all list' 
    dataType: 'json', 
    type: 'get', 
    success: function(data){ 
     var output = ""; 
     $.each(data, function(a,b){ 
      output += "<option value='"+b.id+"'>"+b.parent_select_name+" </option>" 
     $("#parent_select").append(output); 
     }); 
    } 
}) 

/*這將填充你的孩子選擇在改變你的父母*/

$(document).on("change", "#parent_select", function(){ 
    var parent_id = $(this).attr("id"); 

    $.ajax({ 
     url: 'url that will display child data where parent_id = parent_id ', 
     type: 'POST', 
     dataType: 'json', 
     data: { 'parent_id' : parent_id } /* you are passing parent_id*/ 
     success: function(data){ 
       $("#child_select").empty(); 
       var output = ""; 
       $.each(data, function(a,b){ 
        output += "<option id="+b.child_id+">"+b.child_desc+"</option>"; 
       }) 
       $("#child_select").append(output); 
     } 
    }) 
}) 
相關問題