Select values and select values

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.