當我點擊部門安裝主題時,點擊主題時要安裝的服務。但事情並沒有看到我點擊服務時。我認爲描述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>
所以你的問題是,你要顯示第三選擇中的相關服務? –
是的。這是我的問題。第三選擇不起作用 –
好吧,我發佈了一個答案,你在第三選擇中看到的值。 –