Gets and sets the value of the selected item of select

Source: Internet
Author: User

<! DOCTYPE html>

<meta charset= "UTF-8" >

<title> gets the value of the selected item for select </title>

<body>

<select id= "Select0" >

<option value= "0" > Please select options </option>

<option value= "1" > Option one </option>

<option value= "2" > Options two </option>

<option value= "3" > option three </option>

<option value= "4" > Options four </option>

<option value= "5" > Options five </option>

</select>

</body>

<script src= "Jquery-2.1.1.min.js" ></script>

<script>

Window.onload=function () {

var tags=document.getelementsbytagname ("select");

Use JavaScript to get the Select ""

var S=document.getelementbyid ("Select0");

s.selectedindex=2;//to set the index of the selected item

var index= s.selectedindex;//Gets the index of the selected item

Console.log (S.options[index].value);//Output The value of the selected item: value=3

Console.log (S.options[index].text);//Output The value of the selected item in option: Options three

You can also get the following values by using the following method

var Selectedvalue=document.getelementbyid ("Select0"). Value;

Console.log (SelectedValue);


Using the JQuery method to set and get

Console.log ($ ("#select0"). Find ("option:selected"). Text ());//Gets the text inside of the selected option

Console.log ($ ("#select0"). Val ());//Gets the selected value

$ ("#select0"). Val (1);//Set the selected value

Console.log ($ ("#select0"). Val ());


Combination of jquery and JavaScript

var sindex=$ ("#select0"). Get (0). SelectedIndex;

Select the item according to text= ' option four '

$ ("#select0 option[text= ' option One ']"). attr ("Selected", "true");


$ ("#select0 option[value= ' 2 ']"). Remove ();//delete value= ' 2 ' option to delete

Remove all other items except the first item

$ ("#select0 option[value!= ' 0 ']"). Remove ();//can be used to correlate multiple selection boxes, and then clear another option box when one of the options boxes starts to be selected.

};

</script>


This article is from the "Leefeng" blog, make sure to keep this source http://10173384.blog.51cto.com/10163384/1735070

Gets and sets the value of the selected item of select

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.