Select values and select values
Someone asked me a question today.
<body> <select id="tests" onchange="test()"> <option >1</option> <option >2</option> </select> <div id="a" style="display: none">123</div> <div id="b" style="display: ">456</div> <script type="text/javascript"> function test(){ document.getElementById("a").style.display=""; document.getElementById("b").style.display="none"; } </script></body>
You need to select different drop-down lists to display different Divs. Then I improved it for him.
After a long time, I figured out the above method.
At first, the option value is always entangled. Now let's take a look at this opportunity.
<select id="tests" onchange="test(this.value)"> <option value="a">1</option> <option value="b" selected>2</option><select>
Pass the value first. this. value is the selected value, corresponding to the preceding statements a, B
Var obj = document. getElementById ("view ");
Var selectObj = obj. options [obj. selectedIndex]; // obtain the selected object
Var val = selectObj. text; // obtain the selected value 1, 2
Var trueVal = selectObj. value; // internal value a, B
Jquery is a little simpler.
Var txt = $ ("# test"). find ("option: selected"). text (); // 1 2
Var val = $ ("# test"). find ("option: selected"). val (); // a B
Zookeeper
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.