Home > Web Front-end > JS Tutorial > body text

Example code for jQuery operation select_jquery

WBOY
Release: 2016-05-16 17:52:42
Original
1044 people have browsed it

select:

Copy code The code is as follows:

The country that won the 2008 European Cup is :


1. Determine whether the country is selected.
Copy code The code is as follows:

if($("#nation").val( )=="") {
alert("Please select a country");
}

 2. Get the number of option items in the select.
Copy code The code is as follows:
$("#nation").find("option"). length;

 3. Get the value of the selected option and the displayed text.
Copy code The code is as follows:
//Get the value of the selected item
$("#nation ").val();
//Get the text displayed by the selected item
$("#nation").find("option:selected").text();

4. Set Spain as the selected item.
Copy code The code is as follows:
$("#nation").find("option[value= 'Spain']").attr("selected",true);

5. Get the index of the selected country. The index starts from 0.
Copy code The code is as follows:
$("#nation").find("option:selected" ).index()

  6. Add a country option.
?
Copy code The code is as follows:
$("#nation").append(" ");

 7. Delete a country option.
Copy code The code is as follows:
//Delete the option of value="Italy"
$( "#nation").find("option[value='Italy']").remove();
//Remove the option with index 2
$("#nation").find("option [index=2]").remove();
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template