Suppose we will implement the following two select:
Primary <select> {A,B,C}
Secondary<select> { 1,2,3,4,5,6,7,8,9}
Primary->Secondary rule:
A->{1,2,3}
B->{4,5,6}
C->{7,8,9}
Secondary->Primary rule:
1->A
2->A
3->A
4->B
5->B
6->B
7->C
8->C
9->C
The following definitions:
你正在操作的<select> ::= working select
被动的,即将基于你的操作所产生的<select> ::= desired select
In fact, the implementation is according the following considerations: (实现思路)
1. Let both <select> can accept DOM event ,and bind the related event handle function.
2. Configure the mapping rule in the event handle function.
3. In the event function ,create an Array to store the values of the <options> of desired select based on the index you have chosen from the working select
4. Please Note: in event function ,you must set the other <select> ‘s event handle method to be empty in order to prevent it to process its event recursively.
5. Clear all the <options> of desired select.
6. Create the desired select and filling the <options> by the Array created in Step 3.
(Details please see the comments in the javascript file)
Screen Shots:
Primary->Secondary
Initial State:

Step 1: (Choose one from the left, for example B, then the right can only have three choice {4,5,6})

Step 2 (Choose one from the right ,for example “5” and it shows the result)

Secondary->Primary
Initial State:

Step1: (Choose one from the right ,for example “7”)

Step 2: (then the left <select> will display “C” because of the rule “7->C”)

Implementation:
js:
-
-
-
- function createNumbers(){
-
- var alphabetIndex =document.testform.alphabets.selectedIndex;
-
-
- if ((alphabetIndex == null) || (alphabetIndex == 0)) return;
-
-
-
- if (alphabetIndex == 1) {
-
-
- var Numbers = new Array();
- Numbers[0] = new Option("1");
- Numbers[1] = new Option("2");
- Numbers[2] = new Option("3");
-
-
- }
-
- if (alphabetIndex ==2) {
-
- var Numbers = new Array();
- Numbers[0] = new Option("4");
- Numbers[1] = new Option("5");
- Numbers[2] = new Option("6");
-
-
- }
-
- if (alphabetIndex ==3) {
-
- var Numbers = new Array();
- Numbers[0] = new Option("7");
- Numbers[1] = new Option("8");
- Numbers[2] = new Option("9");
-
-
- }
-
-
- for (i=document.testform.numbers.options.length; i>0; i--) {
- document.testform.numbers.options[i] = null;
- }
-
-
- for(i=0; i<Numbers.length; i++) {
- document.testform.numbers.options[i] = Numbers[i];
- }
-
-
- document.testform.numbers.options[0].selected = true;
-
-
-
- document.testform.numbers.onchange=function(){};
-
- }
-
-
-
-
-
- function createAlphabets(){
- var numberIndex =document.testform.numbers.selectedIndex;
- if ((numberIndex == null) || (numberIndex == 0)) return;
-
- if (numberIndex == 1) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("A");
- }
-
- if (numberIndex == 2) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("A");
- }
-
- if (numberIndex == 3) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("A");
- }
-
- if (numberIndex == 4) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("B");
- }
-
- if (numberIndex == 5) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("B");
- }
-
- if (numberIndex == 6) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("B");
- }
-
- if (numberIndex == 7) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("C");
- }
-
- if (numberIndex == 8) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("C");
- }
-
- if (numberIndex == 9) {
-
- var Alphabets = new Array();
- Alphabets[0] = new Option("C");
- }
-
-
-
- for (i=document.testform.alphabets.options.length; i>0; i--) {
- document.testform.alphabets.options[i] = null;
- }
-
- for(i=0; i<Alphabets.length; i++) {
- document.testform.alphabets.options[i] = Alphabets[i];
- }
-
- document.testform.alphabets.options[0].selected = true;
-
- }
The html to render the selection:
- <html>
- <head>
- <title>Test Bi-Directional Linked Select</title>
-
- <script
- type="text/javascript"
- src="js/linkedcombobox.js"
- >
- </script>
- </head>
-
-
- <!--
- Suppose A ->{1,2,3}
- B ->{4,5,6}
- C ->{7,8,9}
-
- 1->A
- 2->A
- 3->A
- 4->B
- 5->B
- 6->B
- 7->B
- 8->B
- 9->B
- -->
-
- <form name="testform">
-
- Test Bi-Directional Select :
-
- <select name="alphabets" onchange="createNumbers()">
-
- <option value=" ">Choose an Alphabet</option>
- <option value="A">A</option>
- <option value="B">B</option>
- <option value="C">C</option>
-
- </select>
-
- <select name="numbers" onchange="createAlphabets()">
-
- <option value="">Choose a Number</option>
- <option value="1">1</option>
- <option value="2">2</option>
- <option value="3">3</option>
- <option value="4">4</option>
- <option value="5">5</option>
- <option value="6">6</option>
- <option value="7">7</option>
- <option value="8">8</option>
- <option value="9">9</option>
-
- </select>
-
- </form>
- </body>
- </html>
本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/840268,如需转载请自行联系原作者