开发者社区> 余二五> 正文

双向二级联动的实现

简介:
+关注继续查看

 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:

 


  1. /** 
  2.  * This function will render the secondary select according to the primart select 
  3.  */ 
  4. function createNumbers(){ 
  5.     //create an alphabetIndex which stands for the selected index from the primary <select> 
  6.     var alphabetIndex =document.testform.alphabets.selectedIndex; 
  7.      
  8.     //if the first index (the prompt is selected) then ignore it 
  9.     if ((alphabetIndex == null) || (alphabetIndex == 0)) return
  10.  
  11.   //binding the <options> based on the index you have chosen from the primary <select> 
  12.    
  13.   if (alphabetIndex == 1) { 
  14.   
  15.   //create an array which can hold all the <options> of the secondary select 
  16.   var Numbers = new Array(); 
  17.   Numbers[0] = new Option("1"); 
  18.   Numbers[1] = new Option("2"); 
  19.   Numbers[2] = new Option("3"); 
  20.    
  21.  
  22.   } 
  23.    
  24.   if (alphabetIndex ==2) { 
  25.      
  26.     var Numbers = new Array(); 
  27.     Numbers[0] = new Option("4"); 
  28.     Numbers[1] = new Option("5"); 
  29.     Numbers[2] = new Option("6"); 
  30.      
  31.      
  32.   } 
  33.    
  34.   if (alphabetIndex ==3) { 
  35.      
  36.     var Numbers = new Array(); 
  37.     Numbers[0] = new Option("7"); 
  38.     Numbers[1] = new Option("8"); 
  39.     Numbers[2] = new Option("9"); 
  40.      
  41.  
  42.   } 
  43.    
  44.   //empty all the existing options from the secondary <select> 
  45.   for (i=document.testform.numbers.options.length; i>0; i--) {  
  46.   document.testform.numbers.options[i] = null
  47.   } 
  48.  
  49.   //set all the <options> of the secondary <select> to be from the created Array  
  50.   for(i=0; i<Numbers.length; i++) { 
  51.   document.testform.numbers.options[i] = Numbers[i]; 
  52.   } 
  53.  
  54.   //set the first <option> of the secondary <select> to be "selected" status 
  55.   document.testform.numbers.options[0].selected = true
  56.    
  57.   //This line is very important 
  58.   //to prevent the event from the secondary<select> that may cause the primary<select> to execute createAlphabets() 
  59.   document.testform.numbers.onchange=function(){}; 
  60.  
  61.  
  62.  
  63. /** 
  64.  * This function will render the primary <select> according to the <secondary> select 
  65.  */ 
  66. function createAlphabets(){ 
  67.     var numberIndex =document.testform.numbers.selectedIndex; 
  68.     if ((numberIndex == null) || (numberIndex == 0)) return
  69.  
  70.   if (numberIndex == 1) { 
  71.  
  72.   var Alphabets = new Array(); 
  73.   Alphabets[0] = new Option("A");  
  74.   } 
  75.    
  76.   if (numberIndex == 2) { 
  77.  
  78.   var Alphabets = new Array(); 
  79.   Alphabets[0] = new Option("A");  
  80.   } 
  81.    
  82.   if (numberIndex == 3) { 
  83.  
  84.   var Alphabets = new Array(); 
  85.   Alphabets[0] = new Option("A");  
  86.   } 
  87.    
  88.   if (numberIndex == 4) { 
  89.  
  90.   var Alphabets = new Array(); 
  91.   Alphabets[0] = new Option("B");  
  92.   } 
  93.    
  94.   if (numberIndex == 5) { 
  95.  
  96.   var Alphabets = new Array(); 
  97.   Alphabets[0] = new Option("B");  
  98.   } 
  99.    
  100.   if (numberIndex == 6) { 
  101.  
  102.   var Alphabets = new Array(); 
  103.   Alphabets[0] = new Option("B");  
  104.   } 
  105.    
  106.   if (numberIndex == 7) { 
  107.  
  108.   var Alphabets = new Array(); 
  109.   Alphabets[0] = new Option("C");  
  110.   } 
  111.    
  112.   if (numberIndex == 8) { 
  113.  
  114.   var Alphabets = new Array(); 
  115.   Alphabets[0] = new Option("C");  
  116.   } 
  117.    
  118.   if (numberIndex == 9) { 
  119.  
  120.   var Alphabets = new Array(); 
  121.   Alphabets[0] = new Option("C");  
  122.   } 
  123.    
  124.   
  125.    
  126.   for (i=document.testform.alphabets.options.length; i>0; i--) {  
  127.   document.testform.alphabets.options[i] = null
  128.   } 
  129.  
  130.   for(i=0; i<Alphabets.length; i++) { 
  131.   document.testform.alphabets.options[i] = Alphabets[i]; 
  132.   } 
  133.  
  134.   document.testform.alphabets.options[0].selected = true
  135.   

The html to render the selection:

 


  1. <html> 
  2. <head> 
  3. <title>Test Bi-Directional Linked Select</title> 
  4.  
  5. <script  
  6.     type="text/javascript"  
  7.     src="js/linkedcombobox.js"  
  8.     > 
  9. </script> 
  10. </head> 
  11.  
  12.  
  13. <!--  
  14. Suppose A ->{1,2,3} 
  15.         B ->{4,5,6} 
  16.         C ->{7,8,9} 
  17.          
  18.         1->
  19.         2->
  20.         3->
  21.         4->
  22.         5->
  23.         6->
  24.         7->
  25.         8->
  26.         9->
  27.  --> 
  28.   
  29. <form name="testform"> 
  30.  
  31. Test Bi-Directional Select : 
  32.  
  33. <select name="alphabets" onchange="createNumbers()"> 
  34.  
  35.   <option value=" ">Choose an Alphabet</option> 
  36.   <option value="A">A</option> 
  37.   <option value="B">B</option> 
  38.   <option value="C">C</option> 
  39.  
  40. </select> 
  41.  
  42. <select name="numbers" onchange="createAlphabets()"> 
  43.  
  44.   <option value="">Choose a Number</option> 
  45.   <option value="1">1</option> 
  46.   <option value="2">2</option> 
  47.   <option value="3">3</option> 
  48.   <option value="4">4</option> 
  49.   <option value="5">5</option> 
  50.   <option value="6">6</option> 
  51.   <option value="7">7</option> 
  52.   <option value="8">8</option> 
  53.   <option value="9">9</option> 
  54.  
  55. </select> 
  56.  
  57. </form> 
  58. </body> 
  59. </html> 




本文转自 charles_wang888 51CTO博客,原文链接:http://blog.51cto.com/supercharles888/840268,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
【Vue 开发实战】基础篇 # 6:双向绑定和单向数据流不冲突
【Vue 开发实战】基础篇 # 6:双向绑定和单向数据流不冲突
13 0
VUE下拉框双向联动
在开发前端页面的时候,常常需要写下拉框,普通常见的下拉框有在页面写死固定值的下拉框,有通过调用后台接口服务而获取的值列表等。无论是原始的jsp页面html页面等,还是现在流行的vue angluar.js等,逻辑都是一样。但是大多数时候我们只需要开发一个下拉框即可,本文讲解VUE页面中,通过实际案例场景讲解多个下拉框如何实现双向动态联动效果。
106 0
javascript实现省市区三级联动选择的代码(数据为模拟json数据)
javascript实现省市区三级联动选择的代码(数据为模拟json数据):
102 0
深入理解vue2.x双向数据绑定原理
深入理解vue2.x双向数据绑定原理
62 0
异步 ”省市县级联“ 的2种方式【Element UI 版】
异步 ”省市县级联“ 的2种方式【Element UI 版】
50 0
前端-vue基础9-双向数据绑定2
前端-vue基础9-双向数据绑定2
33 0
前端-vue基础8-双向数据绑定
前端-vue基础8-双向数据绑定
17 0
实例|APICloud AVM框架封装省市区级联选择弹框
今天介绍用APICloud AVM框架封装省市区级联选择弹框。
55 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载