javascript - Javascript 中的下拉菜单
问题描述
我如何制作一个 javascript 来调用categoryChange
函数,并从中搜索并选择一个选项categoryLists
。
例如$category = "Meeting rooms";
JS代码:
//<![CDATA[
// array of possible countries in the same order as they appear in the category selection list
var categoryLists = new Array(4)
categoryLists["empty"] = ["Select a category"];
categoryLists["Support"] = ["Fast Support", "Remote Desktop", "Printer Maintenance"];
categoryLists["Workplace"] = ["Move Desk", "New Employee preparation"];
categoryLists["New Employee"] = ["Create User"];
categoryLists["Resigned Employee"]= ["Disable user", "Delete user"];
categoryLists["Installation"]= ["Meeting rooms", "Defect Desk/Table", "Prepare Devices", "Reinstallation computer", "Power loss / server breakdown", "New headset"];
categoryLists["Meetings"]= ["IT Internal Meeting", "IT Education"];
categoryLists["Update/Maintenance"]= ["Monthly", "Software Maintenace", "Vmware", "Server OS upgrades/Migrations", "Server Physical upgrades"];
categoryLists["Test"]= ["Windows 10", "Software"];
categoryLists["New Projects"]= [" (Lasernet, Skype, Bitabiz, Astrow)", "Investigate new projects", "New IT securities/scripts"];
/* categoryChange() is called from the onchange event of a select element.
* param selectObj - the select object which fired the on change event.
*/
function categoryChange(selectObj) {
// get the index of the selected option
var idx = selectObj.selectedIndex;
// get the value of the selected option
var which = selectObj.options[idx].value;
// use the selected option value to retrieve the list of items from the categoryLists array
cList = categoryLists[which];
// get the category select element via its known id
var cSelect = document.getElementById("category");
// remove the current options from the category select
var len=cSelect.options.length;
while (cSelect.options.length > 0) {
cSelect.remove(0);
}
var newOption;
// create new options
for (var i=0; i<cList.length; i++) {
newOption = document.createElement("option");
newOption.value = cList[i]; // assumes option string and value are the same
newOption.text=cList[i];
// add the new option
try {
cSelect.add(newOption); // this will fail in DOM browsers but is needed for IE
}
catch (e) {
cSelect.appendChild(newOption);
}
}
}
//]]>
HTML 代码:
Task Type:<select id="category" name="task_type" class="form-control" required>
<option value="0">Select a Task Type</option>
<?php if ($update == true): ?>
<script type="text/javascript">
categoryChange("<?php echo $category; ?>");
</script>
<?php endif ?>
</select>
解决方案
推荐阅读
- android - 错误:清单合并失败:android中的属性application@appComponentFactory
- oauth-2.0 - 由于查询字符串之前的片段标识符,无法访问 Reddit Api 隐式授权流
- android - 如何在 RecyclerView.ItemDecoration() 中使用 dp 而不是像素?
- scala - SBT:自动生成 allOutputFiles
- django - 单独渲染字段时无法访问错误
- oracle - 我在 sqlplus 中创建的表没有出现在 Navicat lite
- python - 我如何将 input() 函数放入 python 3 中的 def 函数中
- javascript - Mongoose - 如何链式保存,以便将数据保存到多个集合中
- android - 在Android上调用接口的方法时如何自动显示进度对话框?
- html - 尽管使用视口等,我的网页不会在不同的屏幕尺寸下全屏显示