如何将值从一个下拉列表存储到另一个下拉列表 [英] How to store value from one dropdown list to another dropdown list

查看:142
本文介绍了如何将值从一个下拉列表存储到另一个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个下拉列表,在第一个下拉列表中,我有一些数据,如果我选择的数据必须存储到第二个下拉列表中。这是代码: -



这是第一个下拉列表,

 < select name =weekIdid =weekIdonchange =getSelected(value)> 
< option value =Select>选择< / option>
< option value =Weekly> Weekly&l​​t; / option>
< option value =Monthly> Monthly< / option>
< option value =Both> Both< / option>
< / select>

这是第二个列表,

 < select id =selectedWeekname =selectedWeeksize =5multiple =multiple> 

如果我在第一个下拉列表中选择每周,则必须将值存储在第二个下拉列表中。如何实现?



提前感谢!

解决方案


,选项;
,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)

这篇关于如何将值从一个下拉列表存储到另一个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆