我想要做的是创建一个下拉菜单,当您选择一个项目时,页面会立即刷新以仅显示用户选择的过滤元素。
这是基本的 HTML:
<SELECT>
<OPTION selected>Show All</OPTION>
<OPTION>Color</OPTION>
<OPTION>Shape</OPTION></SELECT>
注意“显示全部”具有选定的参数。这将是默认选择。
假设我在下面有一个颜色和形状的列表,它们都存储在一个 JSON 表中。两者都将共享同一个名为 $type 的变量:
橙色、六角形、红色、方形、黄色、蓝色、三角形、圆形、绿色、五角形、紫罗兰色
JSON表:
{"table":[
{"name":"orange", "type":"color"},
{"name":"hexagon", "type":"shape"},
{"name":"red", "type":"color"},
{"name":"square", "type":"shape"},
{"name":"yellow", "type":"color"},
{"name":"blue", "type":"color"},
{"name":"triangle", "type":"shape"},
{"name":"circle", "type":"shape"},
{"name":"green", "type":"color"},
{"name":"pentagon", "type":"shape"},
{"name":"violet", "type":"color"}
]}
PHP 将收集将用于过滤器的信息:
for ($i = 0; $i < count($json["table"]); $i++) {
$name = $json["table"][$i]["name"];
$type = $json["table"][$i]["type"];
if ($type == "color") {
// Refresh to show only words of color upon selecting Color from the menu
}
else if ($type == "shape") {
// Refresh to show only words of shape upon selecting Shape from the menu
}
else {
// Refresh to show everything by default or when selecting Show All from the menu
}
}
如何创建此下拉菜单以执行指定的操作?我知道肯定涉及 Javascript,因为这个人发布了一个演示,它准确地演示了我想要什么 - http://jsfiddle.net/trewknowledge/jJZEN/ - 但我不知道如何解决这个问题。