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

Sharing of jquery drop-down select control operation methods (jquery operation select)_jquery

WBOY
Release: 2016-05-16 16:54:29
Original
1085 people have browsed it

JQuery’s methods of getting and setting Select options are summarized as follows:

Code:

Copy code The code is as follows:

$("#select_id").change(function( ){//code...}); //Add an event for Select, which is triggered when one of the items is selected
var checkText=$("#select_id").find("option:selected").text( ); //Get the Text
var checkValue=$("#select_id").val(); //Get the Value
var checkIndex=$("#select_id ").get( 0).selectedIndex; //Get the index value selected by Select
var maxIndex=$("#select_id option:last").attr("index"); //Get the maximum index value of Select
$( "#select_id ").get(0).selectedIndex=1; //Set the item with Select index value 1 to select
$("#select_id ").val(4); //Set the Value value of Select to 4 items are selected
$("#select_id option[text='jQuery']").attr("selected", true); //Set the Text value of Select to jQuery's selected items

Get the text selected by select:

Copy code The code is as follows:

$("#ddlRegType").find("option :selected").text();

Get the value selected by select:

Copy code The code is as follows:

$(" #nowamagic").val();

Get the index selected by select:

Copy code The code is as follows:

$(" #nowamagic").get(0).selectedIndex;

Set select

jQuery adds/removes the Option item of Select:

Copy code The code is as follows:

$("#select_id").append("< ;option value='Value'>Text"); //Add an Option (drop-down item) to Select
$("#select_id").prepend(""); //Insert an Option (first position) for Select
$("#select_id option:last").remove(); //Delete the index value in Select Maximum Option (last one)
$("#select_id option[index='0']").remove(); //Delete the Option (first one) with index value 0 in Select
$( "#select_id option[value='3']").remove(); //Delete the Option
$("#select_id option[text='4']").remove with Value='3' in Select (); //Delete Option
with Text='4' in Select

Set select selected index:

Copy code The code is as follows:

//index For the index value
$("#nowamagic").get(0).selectedIndex=index;

Set select selected value:

Copy code The code is as follows:

$("#nowamagic").attr("value ","Normal");
$("#nowamagic").val("Normal");
$("#nowamagic").get(0).value = value;

Set select selected text:

Copy code The code is as follows:

var count=$("#nowamagicoption").length ;
for(var i=0;i { if($("#nowamagic").get(0).options[i].text == text)
{
             $("#nowamagic").get(0).options[i].selected = true;                                                                    
Clear select:



Copy code

The code is as follows:

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