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.
Firstly, the options list changes in an ugly rectangle
Whereas before it was "normal":
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.
Firstly, the options list changes in an ugly rectangle
Whereas before it was "normal":
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 attributesize
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
1 Answer
Reset to default 5The Styling Issue
Just a note: the
multiple
attribute of theselect
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.
The selection issue
The
selectedOptions
property of theselect
element you get will contain an array ofHTMLOptionElement
s, all of which have thevalue
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
版权声明:本文标题:javascript - select multiple in HTML5 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742390340a2465862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论