且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将值从一个下拉列表存储到另一个下拉列表

更新时间:2023-01-07 21:53:44


,选项;
,selectedWeek = document.getElementById('selectedWeek')
,option;

weekId.onchange = function(){
option = document.createElement('option');
option.value = this.value;
option.text = this.options [this.selectedIndex] .text;
selectedWeek.appendChild(option);
weekId.removeChild(this.options [this.selectedIndex]);
};

在这里看到工作小提琴: http://jsfiddle.net/bKrFK/1/



最后一行事件处理程序将从weekId选择框中删除所选选项(如果不需要则删除该行)


I have 2 dropdown list, where in the first dropdown list i have some data and if I select the data it has to be stored into the second dropdown list. Here is the code :-

This is the first dropdown list,

<select name="weekId" id="weekId" onchange="getSelected(value)">
   <option value="Select">Select</option>
   <option value="Weekly">Weekly</option>
   <option value="Monthly">Monthly</option>
   <option value="Both">Both</option>
</select>

This is the second list,

<select id="selectedWeek" name="selectedWeek" size="5" multiple="multiple">

If I select Weekly in the first dropdown, the value has to get stored in the second dropdown. How do I go about implementing this?

Thanks in advance!!

var weekId = document.getElementById('weekId')
  , selectedWeek = document.getElementById('selectedWeek')
  , option;

weekId.onchange = function() { 
  option = document.createElement('option');
  option.value = this.value;
  option.text = this.options[this.selectedIndex].text;
  selectedWeek.appendChild(option);
  weekId.removeChild(this.options[this.selectedIndex]);  
};​

see working fiddle here: http://jsfiddle.net/bKrFK/1/

the last line in the event-handler will remove the selected option from the weekId select-box (remove that line if not needed)