It is a very common requirement to get the text of a selected box, but,unfortunately,one you submit an action, you can only get the value of a select box.
Below is the way to solve this problem(just hint)
<html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <script> function ss(){ var obj = document.getElementById("sa"); //selectid var index =obj.selectedIndex; alert(obj.options[index].text); } </script> TODO write content <select name="sa" id="sa" onChange="ss(this.selectedIndex)"> <option value="A">aardvark</option> <option value="B">BBC</option> </select> </body> </html>
woooooow,.. sounds very complicated.
ReplyDeleteHi Sparkling,
ReplyDeleteThis is the simplest way I can find. The core codes are just:
var obj = document.getElementById("sa");
var index =obj.selectedIndex;
var text =obj.options[index].text;
If we don't use this way, have to put the values&texts of the select box to an array(map) or use 'value || text' as the value of the option.
Show me if you have simpler solution.