Javascript 子类别SELECT.append(“+msProducts[i]+”); } }else if(业务==“苹果”){ //如果Apple然后遍历appleProducts数组,并将值作为选项元素附加到子类别select元素 对于(var i=0;i
这里有一个有用的工具:这里是实现所需功能的另一种jQuery方法(注释并解释) 添加了引导类来为外观选择元素 HTMLJavascript 子类别SELECT.append(“+msProducts[i]+”); } }else if(业务==“苹果”){ //如果Apple然后遍历appleProducts数组,并将值作为选项元素附加到子类别select元素 对于(var i=0;i,javascript,jquery,dynamic,Javascript,Jquery,Dynamic,这里有一个有用的工具:这里是实现所需功能的另一种jQuery方法(注释并解释) 添加了引导类来为外观选择元素 HTML <select id="mainCategorySelect" name="mainCategorySelect" class="form-control"> <option>Select category</option> </select> <select id="subCategorySelect" name
<select id="mainCategorySelect" name="mainCategorySelect" class="form-control">
<option>Select category</option>
</select>
<select id="subCategorySelect" name="subCategorySelect" class="form-control"></select>
<select name="brand" id="brand">
<option value="-1">--------------------</option>
<option value="apple">Apple</option>
<option value="microsoft">Microsoft</option>
</select>
<select id="model">
<option value="-1">--------------------</option>
</select>
选择类别
JS
// Wait for the dom to be ready
$(function () {
// For the sake of this example our business and products are arrays
var businesses = ["Microsoft","Apple"],
msProducts = ["Microsoft Phone","Microsoft Office","Microsoft Windows 10"],
appleProducts = ["Apple iPhone","Apple iPad","Apple iPod","Apple iSomething"],
// Declare variables for the select elements
mainCategorySelect = $('#mainCategorySelect'),
subCategorySelect = $('#subCategorySelect');
// Iterate thorugh businesses and populate the main select element
for (var i = 0; i < businesses.length; i++) {
mainCategorySelect.append("<option value='"+businesses[i]+"'>"+businesses[i]+"</option>");
}
// using jQuery .on('change')
mainCategorySelect.on('change', function() {
// Always clear the sub category select when main select element value is changed
subCategorySelect.empty();
// Retrieve the value of the main select element
var business = $(this).val();
// if else statement to deside which products to list in the sub category select element
if (business == "Microsoft") {
// if Microsoft then iterate through the msProducts array and append the values as option elements to the sub category select element
for (var i = 0; i < msProducts.length; i++) {
subCategorySelect.append("<option value='"+msProducts[i]+"'>"+msProducts[i]+"</option>");
}
} else if(business == "Apple") {
// if Apple then iterate through the appleProducts array and append the values as option elements to the sub category select element
for (var i = 0; i < appleProducts.length; i++) {
subCategorySelect.append("<option value='"+appleProducts[i]+"'>"+appleProducts[i]+"</option>");
}
}
// When the user changes the value of the sub category select element the do something with it
subCategorySelect.on('change', function() {
alert($(this).val());
});
});
});
//等待dom准备就绪
$(函数(){
//在本例中,我们的业务和产品是阵列
var Business=[“微软”、“苹果”],
msProducts=[“Microsoft手机”、“Microsoft Office”、“Microsoft Windows 10”],
appleProducts=[“苹果iPhone”、“苹果iPad”、“苹果iPod”、“苹果东西”],
//为select元素声明变量
mainCategorySelect=$(“#mainCategorySelect”),
子类别选择=$(“#子类别选择”);
//迭代thorugh业务并填充主选择元素
对于(var i=0;i
这里有一个可行的办法:如果产品线扩展超过一定数量,那么使用明确的条件进行验证将使这一点在将来难以维持。如其他人所示,使用某种形式的数组是一种更好的方法 另外,尽管jQuery是一个很棒的库,但不要忘记香草JavaScript。如果代码编写得好,即使它看起来更复杂,普通JavaScript也应该比jQuery的运行速度更快。考虑到这一点,这里有另一个解决方案,这次是“或多或少”的纯JavaScript——我在准备就绪的时候离开了 HTML
<select id="mainCategorySelect" name="mainCategorySelect" class="form-control">
<option>Select category</option>
</select>
<select id="subCategorySelect" name="subCategorySelect" class="form-control"></select>
<select name="brand" id="brand">
<option value="-1">--------------------</option>
<option value="apple">Apple</option>
<option value="microsoft">Microsoft</option>
</select>
<select id="model">
<option value="-1">--------------------</option>
</select>
--------------------
苹果
微软
--------------------
JavaScript
var products = {
apple: [
{ name: "iPhone 6 Plus", model: "iphone_6plus" },
{ name: "iPhone 6", model: "iphone_6" },
{ name: "iPhone 5s", model: "iphone_5s" },
{ name: "iPhone 5c", model: "iphone_5c" }
],
microsoft: [
{ name: "Windows 10", model: "windows_10" },
{ name: "Windows 8", model: "windows_8" },
{ name: "Office 2015", model: "office_2015" },
{ name: "Office 2014", model: "office_2014" }
]
};
function create_option(text, value) {
var option = document.createElement("option");
var txt = document.createTextNode(text);
option.value = value;
option.appendChild(txt);
return option;
}
function populate_model(selection) {
var select = document.getElementById("model");
var i, l;
if ((selection == -1) || (products[selection] === undefined))
return false;
while (select.lastChild)
select.removeChild(select.lastChild);
select.appendChild(document.createElement("option").appendChild(document.createTextNode("--------------------")));
for (i = 0, l = products[selection].length; i < l; i++)
select.appendChild(create_option(products[selection][i].name, products[selection][i].model));
}
$(document).ready(function() {
var brand = document.getElementById("brand");
brand.onchange = function() {
populate_model(this.options[this.selectedIndex].value);
};
brand.value = -1;
});
var产品={
苹果公司:[
{名称:“iphone6plus”,型号:“iphone6plus”},
{名称:“iPhone 6”,型号:“iPhone_6”},
{名称:“iPhone 5s”,型号:“iPhone_5s”},
{名称:“iPhone 5c”,型号:“iPhone_5c”}
],
微软:[
{名称:“Windows 10”,型号:“Windows_10”},
{名称:“Windows 8”,型号:“Windows_8”},
{名称:“Office 2015”,型号:“Office_2015”},
{名称:“Office 2014”,型号:“Office_2014”}
]
};
函数创建_选项(文本、值){
var option=document.createElement(“选项”);
var txt=document.createTextNode(文本);
option.value=值;
option.appendChild(txt);
返回选项;
}
函数类型(选择){
var select=document.getElementById(“模型”);
变量i,l;
if((选择==-1)| |(产品[选择]==未定义))
返回false;
while(选择.lastChild)
select.removeChild(select.lastChild);
选择.appendChild(document.createElement(“选项”).appendChild(document.createTextNode(“--------------------------”));
对于(i=0,l=products[selection]。长度;i
我还更新了您的JSFIDLE:使用显式条件进行验证,如果产品线扩展超过一定数量,这将使将来很难维护。如其他人所示,使用某种形式的数组是一种更好的方法 另外,尽管jQuery是一个很棒的库,但不要忘记香草JavaScript。如果代码编写得好,即使它看起来更复杂,普通JavaScript也应该比jQuery的运行速度更快。考虑到这一点,这里有另一个解决方案,这次是“或多或少”的纯JavaScript——我在准备就绪的时候离开了 HTML
<select id="mainCategorySelect" name="mainCategorySelect" class="form-control">
<option>Select category</option>
</select>
<select id="subCategorySelect" name="subCategorySelect" class="form-control"></select>
<select name="brand" id="brand">
<option value="-1">--------------------</option>
<option value="apple">Apple</option>
<option value="microsoft">Microsoft</option>
</select>
<select id="model">
<option value="-1">--------------------</option>
</select>
--------------------
苹果
微软
--------------------
JavaScript
var products = {
apple: [
{ name: "iPhone 6 Plus", model: "iphone_6plus" },
{ name: "iPhone 6", model: "iphone_6" },
{ name: "iPhone 5s", model: "iphone_5s" },
{ name: "iPhone 5c", model: "iphone_5c" }
],
microsoft: [
{ name: "Windows 10", model: "windows_10" },
{ name: "Windows 8", model: "windows_8" },
{ name: "Office 2015", model: "office_2015" },
{ name: "Office 2014", model: "office_2014" }
]
};
function create_option(text, value) {
var option = document.createElement("option");
var txt = document.createTextNode(text);
option.value = value;
option.appendChild(txt);
return option;
}
function populate_model(selection) {
var select = document.getElementById("model");
var i, l;
if ((selection == -1) || (products[selection] === undefined))
return false;
while (select.lastChild)
select.removeChild(select.lastChild);
select.appendChild(document.createElement("option").appendChild(document.createTextNode("--------------------")));
for (i = 0, l = products[selection].length; i < l; i++)
select.appendChild(create_option(products[selection][i].name, products[selection][i].model));
}
$(document).ready(function() {
var brand = document.getElementById("brand");
brand.onchange = function() {
populate_model(this.options[this.selectedIndex].value);
};
brand.value = -1;
});
var产品={
苹果公司:[
{名称:“iphone6plus”,型号:“iphone6plus”},
{名称:“iPhone 6”,型号:“iPhone_6”},
{名称:“iPhone 5s”,型号:“iPhone_5s”},
{名称:“iPhone 5c”,型号:“iPhone_5c”}
],
微软:[
{nam