How to store value from one dropdown list to anoth

2019-07-22 11:04发布

问题:

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!!

回答1:

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)



回答2:

You can do this using Javascript:

function listbox_moveacross(sourceID, destID) {
    var src = document.getElementById(sourceID);
    var dest = document.getElementById(destID);

    for(var count=0; count < src.options.length; count++) {

        if(src.options[count].selected == true) {
                var option = src.options[count];

                var newOption = document.createElement("option");
                newOption.value = option.value;
                newOption.text = option.text;
                newOption.selected = true;
                try {
                         dest.add(newOption, null); //Standard
                         src.remove(count, null);
                 }catch(error) {
                         dest.add(newOption); // IE only
                         src.remove(count);
                 }
                count--;
        }
    }
}

Pass this function with ids of your selectbox.

For Demo: Listbox move left-right options JavaScript



回答3:

Try this..

<html>
<body>
<select name="weekId" id="weekId" onchange="document.getElementById('selectedWeek').value=this.value">
   <option value="Select">Select</option>
   <option value="Weekly">Weekly</option>
   <option value="Monthly">Monthly</option>
   <option value="Both">Both</option>
</select>
<select id="selectedWeek" name="selectedWeek" size="5" multiple="multiple">

<option value="Select">Select</option>
   <option value="Weekly">Weekly</option>
   <option value="Monthly">Monthly</option>
   <option value="Both">Both</option>
 </select>
</body>
</html>

Note : Second drop down has all value available in first select box.



回答4:

First of all, when you call the javascript function in onChange event, replace getSelected(value) to getSelected(this.value).

Now after that,

Your javascript function getSelected(value) should look like this

function getSelected(value)
{
     document.getElementById("selectedWeek").innerHTML = '<option value="'+value+'">'+value+'</option>';
}


回答5:

You could use the following, without any jQuery dependencies. I've added some comments to explain what is going on.

<script>
    function handleSelection(weekDropDown) {
            // Get selected value
        var selection = weekDropDown.options[weekDropDown.selectedIndex].value;

        var selectedWeekDropDown = document.getElementById("selectedWeek");

        var opt;
        if(selectedWeekDropDown.options[0]) {
            // Replace
            opt = selectedWeekDropDown.options[0];
        } else {
            // Add an option
            opt = document.createElement("option");
        }

        if(!selectedWeekDropDown.options[0]) {
            selectedWeekDropDown.options.add(opt);
        }

        // Set the option text and value
        opt.text = selection;
        opt.value = selection;

    }
</script>

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

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