JS Gets the selected value of the Select tag

Source: Internet
Author: User

<select name= "" id= "Testselect" onclick= "Test2 ()" >
<option value= "1" >a</option>
<option value= "2" >b</option>
<option value= "3" >c</option>
<option value= "4" >d</option>
</select>
<script src= "Js/jquery.min.js" ></script>
<script>


function Test () {
var obj = document.getElementById ("Testselect"); Location ID
Console.log (obj);
var index = Obj.selectedindex; Check Index
Console.log (index);
var text = Obj.options[index].text; Select text
Console.log (text);
var value = Obj.options[index].value; Checked values
Console.log (value);
}
function Test1 () {
Console.log ($ (' #testSelect option:selected '). text ());//Selected texts

Console.log ($ (' #testSelect option:selected '). Val ());//The selected value

Console.log ($ ("#testSelect"). Get (0). SelectedIndex);//Index
}
function Test2 () {
Console.log ($ ("#testSelect"). Find ("option:selected"). Text ());//Selected
Console.log ($ ("#testSelect"). Find ("option:selected"). Val ());
Console.log ($ ("#testSelect"). Get (0). SelectedIndex);
}
</script>

JS Gets the selected value of the Select tag

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.