简体   繁体   English

Javascript根据另一个下拉列表的另一个值从数组中填充下拉列表:

[英]Javascript filling dropdown from array based on another value of another dropdown:

I have two dropdown's. 我有两个下拉菜单。 The 2nd dropdown should filter the data based on the 'value' selected in the 1st dropdown.both the dropdown's are dynamically populated from the backend 第二个下拉列表应根据第一个下拉列表中选择的“值”过滤数据。下拉列表是从后端动态填充的

Say for example: 1st dropdown you choose any student, the value field of the dropdown contains the student no & based on the student selection made, the 2nd dropdown should display the subjects of that particular student. 例如:您选择任何学生的第一个下拉列表,下拉列表的值字段包含学生否和基于学生选择,第二个下拉列表应显示该特定学生的科目。 The data to be displayed in the dropdown is added from the backend (mainframe prog's). 要在下拉列表中显示的数据是从后端(大型机编程)添加的。 I've added the student no at the end of the 2nd dropdown data separated by '*'. 我在第二个下拉数据末尾添加了学生号,用'*'分隔。 I sort the data based on student no from the backend before passing it onto screen 在将数据传递到屏幕之前,我会根据后端的学生数对数据进行排序

This is my first try at html & javascript code changes(I'm a mainframe developer).Hence I'm sure there is a easier and better solution. 这是我第一次尝试html和javascript代码更改(我是大型机开发人员).Hence我确信有一个更简单,更好的解决方案。 We use a in house tool which automatically generates the HTML. 我们使用自动生成HTML的内部工具。 I just have to give the dropdown-ID , other code such as 'select', 'options' etc is automatically generated. 我只需要提供下拉ID,其他代码如'select','options'等会自动生成。 Hence after filtering the data from dropdown2 should be hidden and a new dropdown should be displayed which does not contain the 'student no' and '*' which i have added. 因此,在过滤掉dropdown2中的数据后,应该隐藏,并且应该显示一个新的下拉列表,其中不包含我添加的'student no'和'*'。

if student1 is selected from the 1st dropdown then the second dropdwon should display Subject 1 Subject 2 如果从第一个下拉列表中选择student1,则第二个dropdwon应显示Subject 1 Subject 2

    HTML
<div id="dropdrop1">
<select name="StudentC" size="1" onchange="Change()" id="studentCID">
<option value="ALL">All Students</option>
<option value="0123456789">0123456789 - Student 1</option>
<option value="0023456789">0023456789 - Student 2 </option>
</select> </div>

<div id="dropdown2">
<select name="StuclassC" size="1" id="StuclassCID">
<option value="ALL">All Class</option>
<option value="1111111111">1111111111 - Subject 1 0123456789</option>
<option value="2222222222">2222222222 - Subject 2 0123456789</option>
<option value="3333333333">3333333333 - Subject 3 0023456789</option>
<option value="4444444444">4444444444 - Subject 4 0023456789</option>

<select name=newdropdown  id= "newdropdown"></select> 

and there is a search button after this, which populates a grid. 此后有一个搜索按钮,用于填充网格。

Javascript

    Change()
{
// text from  dropdown2
var aname = document.getElementById("StuclassCID");
var aopts = aname.options;
var adtext = new Array();
var advalue = new Array(); 
for(i = 0; i < aopts.length; i++)
{
    adtext.push(aopts[i].text);
    advalue.push(aopts[i].value);
}

//copy selected student no from dropdown1

var StuDD = document.getElementById("studentCID");
var Stuno = StuDD.options[StuDD.selectedIndex].value;


//Search for '*' in each string from dropdown2

var i,j,temp,Stunogreater;
var studrop = new Array();
var stunoa = new Array();
var showarray = new Array();
var showvalue = new Array();

var Stunogreater =False;

//Copying 'All Class' text dropdown2

 studrop.push(adtext[0]);

//separate the displayable portion from student no in the dropdown2

for (var i = 1; i < adtext.length; i++)
 {
     temp = adtext[i]; 
     var n=temp.indexOf("*");
     studrop.push(temp.substr(0, n-1));
     stunoa.push(temp.substr(n+1,10));
 }    


while( Stunogreater == False )
{
for(var j =0; j < studrop.length;j++)
 {
   if( stunoa[j] == stuno)    
     {
       showarray.push( studrop[j]);
       showvalue.push(stunoa[j]);
      }
   if (stunoa[j] > stuno)  Stunogreater = True;
  }
}         


//Copy text and value to show in a new dropdown

var sel = document.getElementById('newdropdown');
for(var i = 0; i < showarray.length; i++) {
    var opt = document.createElement('option');
    opt.innerHTML = showarray[i];
    opt.value = showvalue[i];
    sel.appendChild(opt);
}

Well i dont know if this code is gonna work as the text environment is currently unavailable untill next week. 好吧,我不知道这个代码是否会起作用,因为文本环境目前不可用,直到下周。 And there is a search button after dropdown2 ie the last dropdown, when the user clicks on search. dropdown2之后有一个搜索按钮,即用户点击搜索时的最后一个下拉列表。 The data in the 'Value' fields is passed on to the beackend to load a grid. “值”字段中的数据被传递到beackend以加载网格。 So basically the backend populates all possible values for each of the dropdown and when the users clicks on search the 'Value' field of each of the dropdwon is used to generate the grid. 所以基本上后端为每个下拉列表填充所有可能的值,当用户点击搜索时,每个dropdwon的'Value'字段用于生成网格。

Any help is appreciated in guiding me to do this in a efficient way.Please provide code snippets along with your sugesstions as its possible that i might take a long time to figure out what you are tryin to say as i'm new to front end languages. 任何帮助都是指导我以有效的方式做到这一点。请提供代码片段以及您的sugesstions作为可能,我可能需要很长时间才能弄清楚你试着说什么,因为我是前端的新手语言。 PS: I can use only HTML and Javascript (no to Php, Jquery & others). PS:我只能使用HTML和Javascript(不对Php,Jquery等)。

If you can simply hide the values not being used, you can do this with a relatively simple check: http://jsfiddle.net/MaxPRafferty/SEmwG/ html: 如果您可以简单地隐藏未使用的值,则可以通过相对简单的检查来执行此操作: http//jsfiddle.net/MaxPRafferty/SEmwG/ html:

<select id="dropdown1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>

<select id="dropdown2">
<option>1</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
​

css: CSS:

.hiddenElement{display:none;}​

js: JS:

var dropdown1 = document.getElementById("dropdown1");
var dropdown2 = document.getElementById("dropdown2");
var change = function() {
    alert("here");
    for (var i = 0; i < dropdown2.length; i++) {
        if (dropdown2[i].value.indexOf(dropdown1.value) === -1) {
            dropdown2[i].className = "hiddenElement";
        }
        else {
            dropdown2[i].className = "";
        }
    }
}

dropdown1.onchange = change;​
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据使用Javascript在另一个下拉菜单中选择的内容设置下拉菜单值 - Set dropdown value based on what is selected in another dropdown using Javascript 如何根据另一个Dropdown使用javascript更改下拉列表值? - How to change Dropdown lists value based on another Dropdown using javascript? 根据同一 html 表单中的另一个下拉列表填充下拉列表 - Filling a dropdown list based on another dropdown list in the same html form 基于另一个下拉JavaScript的下拉值过滤器 - Dropdown values filter based on another dropdown javascript 根据另一个下拉列表选择JavaScript过滤下拉列表 - Filtering dropdown based on another dropdown selection Javascript 根据在另一个下拉列表中选择的值填充下拉列表 - Populating a dropdown based on the value selected in another dropdown 根据另一个下拉列表更改下拉选择值? - Change dropdown selection value based on another dropdown? 根据另一个下拉列表选择一个下拉列表的值 - select value of one dropdown based on another dropdown 根据另一个下拉值获取下拉数据 - Get data on dropdown based on another dropdown value 根据使用Javascript在另一个下拉列表中选择的内容设置下拉值,反之亦然 - Set dropdown value based on what is selected in another dropdown using Javascript, and vice versa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM