Json下拉列表

问题描述:

当我点击部门安装主题时,点击主题时要安装的服务。但事情并没有看到我点击服务时。我认为描述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

好吧,我发布了一个答案,你在第三选择中看到的值。 –

我编辑你的代码,现在它的工作原理(通过选择第一个“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。 –

尝试var data = eval('your json-string'),这应该工作,我认为。

+1

如果你听道格拉斯克罗克福德,'eval'是邪恶的。您应该考虑使用json2.js脚本并调用'var data = JSON.parse('your_json_string');'来代替。它将使您免受潜在的脚本攻击或恶意“json”数据的侵害。 –

+0

感谢您的回答。但我无法得到结果。更改第二个选择框在第三个选择框中显示此值。我怎样才能做到这一点?。 –

+0

嗯..我可能误会了这个问题:/ –