最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the value of a multiple option dropdown? - Stack Overflow

programmeradmin3浏览0评论

Say I have this dropdown:

<select name="color" multiple="multiple">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>

So basically more than 1 color can be selected. What I'd like is that if a user selects red, and then clicks green, i'd like a function to be called each time which pops up a message box saying the color which was most recently clicked.

I've tried this:

<option value="red" onclick="alert('red');">Red</option>
<option value="green" onclick="alert('green');">Green</option>
<option value="blue" onclick="alert('blue');">Blue</option>

This works in firefox and chrome, but not in IE.

Any ideas?

Say I have this dropdown:

<select name="color" multiple="multiple">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>

So basically more than 1 color can be selected. What I'd like is that if a user selects red, and then clicks green, i'd like a function to be called each time which pops up a message box saying the color which was most recently clicked.

I've tried this:

<option value="red" onclick="alert('red');">Red</option>
<option value="green" onclick="alert('green');">Green</option>
<option value="blue" onclick="alert('blue');">Blue</option>

This works in firefox and chrome, but not in IE.

Any ideas?

Share Improve this question edited Feb 14, 2010 at 15:25 Ali asked Feb 14, 2010 at 15:12 AliAli 267k268 gold badges591 silver badges784 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 2
$("select[name='color']").change(function() {
    // multipleValues will be an array
    var multipleValues = $(this).val() || [];

    // Alert the list of values
    alert(multipleValues[multipleValues.length - 1]);
});

Here's another examples: http://api.jquery./val/

The following code should do what I think you're after. Each time an item is selected, it pares the current list of selections against the previous list, and works out which items have changed:

<html>
  <head>
    <script type="text/javascript">
      function getselected(selectobject) {
        var results = {};
        for (var i=0; i<selectobject.options.length; i++) {
          var option = selectobject.options[i];
          var value = option.value;
          results[value] = option.selected;
        }
        return results;
      }

      var currentselect = {};

      function change () {
        var selectobject = document.getElementById("colorchooser");
        var newselect = getselected(selectobject);
        for (var k in newselect) {
          if (currentselect[k] != newselect[k]) {
            if (newselect[k]) {
              alert("Option " + k + " selected");
            } else {
              alert("Option " + k + " deselected");
            }
          }
        } 
        currentselect = newselect;
      } 
    </script>
  </head>
  <body>
    <select id="colorchooser"
            name="color"
            multiple="multiple"
            onchange='javascript:change();'
            >
      <option value="red">Red</option>
      <option value="green">Green</option>
      <option value="blue">Blue</option>
    </select>
  </body>
</html>

It should work just as well in Internet Explorer as Firefox et al.

Since you using jQuery,I suggest you to take a look at this superb plugins. This plugins will transform a multiple select dropdown into a checkbox list, so user can select multiple values with easy.

To get the values, I suggest you use fieldValue methods from jQuery form plugins. It's a robust way to get value from any type of form element. Beside, you can use this plugins to submit your form via AJAX easily.

This will alert only the last (most recent) selected value. Calling $(this).val() using the select's change handler will return an array of all your selected values:

$(document).ready(function() {
     $("select[name=color] option").click(function() {
        alert($(this).val());
    });
});

I am not sure what you exactly want. This will always alert the last selected color:

$(function(){
    var selected = Array();
    $('select[name=color] option').click(function() {
        if($(this).is(':selected')) {
           selected.push($(this).val());
        }
        else {
           for(var i = 0; i < selected.length;i++) {
               if(selected[i] == $(this).val()) {
                   selected = selected.splice(i,1);
               }
           }
        }  
        alert(selected[selected.length -1])
    });         
});

The array is used to maintain the history of selected colors.

For the last clicked color, it is simpler:

$(function(){
    $('select[name=color] option').click(function() {
        alert($(this).val());
    });         
});

This is so plicated to acplish that I used a simpler option of listing the items with a checkbox next to them and a select/unselect all button. That works much better and is also supported by IE. Thanks to everyone for their answers.

发布评论

评论列表(0)

  1. 暂无评论