admin管理员组

文章数量:1335727

I'm new to HTML5 and I'm trying to test the <select> with the attribute multiple in forms on Google Chrome. I encounter two problems.

  1. Firstly, the options list changes in an ugly rectangle

    Whereas before it was "normal":

  2. My second problem is that, it seems that when i want to get the values of the select (by clicking on the button and in the code using javascript), only one is given...

Here is my code:

<!DOCTYPE html>
<html>
<body>

How do you travel?
<form  method="get" id=myForm" onsubmit="done();">
<select name="transport" multiple> <optgroup label="Ecological">
<option value="Feet" selected>By Foot</option>
<option value="Bike">By Bike</option> </optgroup>
<optgroup label="Non-ecological">
<option value="public transports">With public transports</option> <option value="motorbike">By motorbike</option> <option value="car">By car</option>
</optgroup> </select>

<button onclick="bdone();">button</button>

<script>

function bdone(){


var mesOptions=document.getElementsByTagName('select')[0];
alert(mesOptions.value); 
}


</script>
</body>
</html>

Thank you for reading me!

I'm new to HTML5 and I'm trying to test the <select> with the attribute multiple in forms on Google Chrome. I encounter two problems.

  1. Firstly, the options list changes in an ugly rectangle

    Whereas before it was "normal":

  2. My second problem is that, it seems that when i want to get the values of the select (by clicking on the button and in the code using javascript), only one is given...

Here is my code:

<!DOCTYPE html>
<html>
<body>

How do you travel?
<form  method="get" id=myForm" onsubmit="done();">
<select name="transport" multiple> <optgroup label="Ecological">
<option value="Feet" selected>By Foot</option>
<option value="Bike">By Bike</option> </optgroup>
<optgroup label="Non-ecological">
<option value="public transports">With public transports</option> <option value="motorbike">By motorbike</option> <option value="car">By car</option>
</optgroup> </select>

<button onclick="bdone();">button</button>

<script>

function bdone(){


var mesOptions=document.getElementsByTagName('select')[0];
alert(mesOptions.value); 
}


</script>
</body>
</html>

Thank you for reading me!

Share Improve this question edited Mar 16, 2013 at 18:49 Aaron Blenkush 3,0592 gold badges29 silver badges54 bronze badges asked Mar 16, 2013 at 18:28 PaoPao 2063 silver badges10 bronze badges 3
  • "Ugly" is subjective, but the change in UI is something you have to live with. There isn't a standard UI widget which provides a way to select multiple items from a drop down menu. The ListBox style widget is the standard. (The alternative to living with it is to build you own UI widget (which won't be recognised by users) out of other elements and JavaScript.) – Quentin Commented Mar 18, 2013 at 7:35
  • To your question #1 it appears like that because you included the multiple attribute in your <select> tag. Now if you don't want it to have the scrollbars, you can just add the attribute size eg: <select multiple size="7"> – lozadaOmr Commented Mar 12, 2014 at 13:23
  • "My second problem" that would be two questions. – Raedwald Commented Oct 16, 2015 at 11:28
Add a ment  | 

1 Answer 1

Reset to default 5
  1. The Styling Issue

    Just a note: the multiple attribute of the select element is not specifically HTML5.

    The styling is going to depend on the CSS styles that are being applied, both the user agent styles (the default browser styles), and the specific CSS on that page. Try putting it in a page by itself (or in a jsFiddle and see if you get the same styling.

  2. The selection issue

    The selectedOptions property of the select element you get will contain an array of HTMLOptionElements, all of which have the value property. See below:

    jsFiddle

    function bdone(){
        var selectElem = document.getElementsByTagName('select')[0]
        var mesOptions = selectElem.selectedOptions;
        for(var a=0;a<mesOptions.length;a++) {
            alert(mesOptions[a].value);
        }
    }

本文标签: javascriptselect multiple in HTML5Stack Overflow