2011-08-27 52 views
3

當我點擊部門安裝主題時,點擊主題時要安裝的服務。但事情並沒有看到我點擊服務時。我認爲描述json是不準確的。你能幫我解決這個問題嗎?謝謝。 我的jquery代碼;Json下拉列表

/* <![CDATA[ */ 

(function($) { 

$.fn.changeType = function(){ 

    var data = [ 
     { 
     "department": "IT", 
     "subject": [ 
       {"title":"Programmer", 
         "services" :[ 
         {"name":"example1"}, 
         {"name":"example2"} 
            ] 

       }, 
       {"title":"Solutions Architect"}, 
       {"title":"Database Developer"} 
       ] 
     }, 
     {"department": "Accounting", 
     "subject": [ 
       {"title":"Accountant"}, 
       {"title":"Payroll Officer"}, 
       {"title":"Accounts Clerk"}, 
       {"title":"Analyst"}, 
       {"title":"Financial Controller"} 
       ] 
     }, 
     { 
     "department": "HR", 
     "subject": [ 
       {"title":"Recruitment Consultant"}, 
       {"title":"Change Management"}, 
       {"title":"Industrial Relations"} 
       ] 
     }, 
     { 
     "department": "Marketing", 
     "subject": [ 
       {"title":"Market Researcher"}, 
       {"title":"Marketing Manager"}, 
       {"title":"Marketing Co-ordinator"} 
       ] 
     } 
     ] 

     var options_departments = '<option>Select<\/option>'; 
     $.each(data, function(i,d){ 
      options_departments += '<option value="' + d.department + '">' + d.department + '<\/option>'; 
     }); 
     $("select#departments", this).html(options_departments); 

     $("select#departments", this).change(function(){ 
      var index = $(this).get(0).selectedIndex; 
      var d = data[index-1]; // -1 because index 0 is for empty 'Select' option 
      var options = ''; 
      if (index > 0) { 
       $.each(d.subject, function(i,j){ 
          options += '<option value="' + j.title + '">' + j.title + '<\/option>'; 
       }); 
      } else { 
       options += '<option>Select<\/option>'; 
      } 
      $("select#subject").html(options); 
     }) 
}; 


     $("select#subject", this).change(function(){ 
      var index = $(this).get(0).selectedIndex; 
      var j = data[index-1]; // -1 because index 0 is for empty 'Select' option 
      var options = ''; 
      if (index > 0) { 
       $.each(j.services, function(i,b){ 
          options += '<option value="' + b.name + '">' + b.name + '<\/option>'; 
       }); 
      } else { 
       options += '<option>Select<\/option>'; 
      } 
      $("select#services").html(options); 
     }) 




})(jQuery); 

$(document).ready(function() { 
    $("form#search").changeType(); 
}); 

/* ]]> */ 

我的html代碼;

<form id="search" action="" name="search"> 
    <select name="departments" id="departments"> 
     <option>Select</option> 
    </select> 

    <select name="subject" id="subject"> 
     <option>Select</option> 
    </select> 

    <select name="services" id="services"> 
     <option>Select</option> 
    </select> 

</form> 
+0

所以你的問題是,你要顯示第三選擇中的相關服務? –

+0

是的。這是我的問題。第三選擇不起作用 –

+0

好吧,我發佈了一個答案,你在第三選擇中看到的值。 –

回答

1

我編輯你的代碼,現在它的工作原理(通過選擇第一個「IT」,然後選擇小提琴中的「程序員」)。當然,如果沒有「服務」,則第三項選擇中不會顯示任何內容。 你應該增加一些邏輯,以便它顯示了第三隻如果有涉及到你的主題

(function($) { 


    var data = [ 
     { 
     "department": "IT", 
     "subject": [ 
      { 
      "title": "Programmer", 
      "services": [ 
       { 
       "name": "example1"}, 
      { 
       "name": "example2"} 
          ] 

      }, 
     { 
      "title": "Solutions Architect"}, 
     { 
      "title": "Database Developer"} 
     ]}, 
    { 
     "department": "Accounting", 
     "subject": [ 
      { 
      "title": "Accountant"}, 
     { 
      "title": "Payroll Officer"}, 
     { 
      "title": "Accounts Clerk"}, 
     { 
      "title": "Analyst"}, 
     { 
      "title": "Financial Controller"} 
     ]}, 
    { 
     "department": "HR", 
     "subject": [ 
      { 
      "title": "Recruitment Consultant"}, 
     { 
      "title": "Change Management"}, 
     { 
      "title": "Industrial Relations"} 
     ]}, 
    { 
     "department": "Marketing", 
     "subject": [ 
      { 
      "title": "Market Researcher"}, 
     { 
      "title": "Marketing Manager"}, 
     { 
      "title": "Marketing Co-ordinator"} 
     ]} 
    ] 
    var selectedDepartment, selectedSubject; 

    $.fn.changeType = function() { 

     var options_departments = '<option>Select<\/option>'; 
     $.each(data, function(i, d) { 
      options_departments += '<option value="' + d.department + '">' + d.department + '<\/option>'; 
     }); 
     $("select#departments", this).html(options_departments); 

     $("select#departments").change(function() { 
      var index = $(this).get(0).selectedIndex; 

      var d = data[index - 1]; // -1 because index 0 is for empty 'Select' option 
      selectedDepartment = d; 
      var options = ''; 
      if (index > 0) { 
       options += '<option>Select<\/option>'; 
       $.each(d.subject, function(i, j) { 
        options += '<option value="' + j.title + '">' + j.title + '<\/option>'; 
       }); 
      } else { 
       options += '<option>Select<\/option>'; 
      } 
      $("select#subject").html(options); 
     }) 
    }; 


    $("select#subject").change(function() { 
     var index = $(this).get(0).selectedIndex; 
     selectedSubject = selectedDepartment.subject[index -1]; 
     var options = ''; 
     if (index > 0) { 
      $.each(selectedSubject.services, function(i, b) { 
       options += '<option value="' + b.name + '">' + b.name + '<\/option>'; 
      }); 
     } else { 
      options += '<option>Select<\/option>'; 
     } 
     $("select#services").html(options); 
    }) 




})(jQuery); 

$(document).ready(function() { 
    $("form#search").changeType(); 
}); 

小提琴這裏的服務選擇:

http://jsfiddle.net/fF38L/

編輯 - 使其工作在IE8起飛的console.log()

http://jsfiddle.net/fF38L/1/

+0

Nicola,謝謝你真的對我的問題表現出興趣。但運行此代碼的http://jsfiddle.net/fF38L/不能再次運行。當我選擇IT展會(程序員,解決方案架構師,數據庫開發人員)時。但是當我選擇程序員不顯示(example1,example2) –

+0

它適用於我:你使用什麼瀏覽器?我測試了它在Firefox和鉻,它的作品 –

+0

我的瀏覽器是ie8。 –

0

嘗試var data = eval('your json-string'),這應該工作,我認爲。

+1

如果你聽道格拉斯克羅克福德,'eval'是邪惡的。您應該考慮使用json2.js腳本並調用'var data = JSON.parse('your_json_string');'來代替。它將使您免受潛在的腳本攻擊或惡意「json」數據的侵害。 –

+0

感謝您的回答。但我無法得到結果。更改第二個選擇框在第三個選擇框中顯示此值。我怎樣才能做到這一點?。 –

+0

嗯..我可能誤會了這個問題:/ –