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

How to operate select with jquery

coldplay.xixi
Release: 2023-01-04 09:37:02
Original
3478 people have browsed it

Jquery operation select method: 1. Set the item with value to pxx to select, the code is [$(".selector").val("pxx")]; 2. Set the item with text to pxx to select , the code is [$(".selector").find("option[te].

How to operate select with jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version , thinkpad t480 computer.

Related free learning recommendations: javascript learning tutorial, jQuery Tutorial

Jquery operation select method:

1. Set the item with value to pxx to select

$(".selector").val("pxx");
Copy after login

2. Select the item with text set to pxx

$(".selector").find("option[text='pxx']").attr("selected",true);
Copy after login

There is a usage of square brackets. The equal sign in the brackets is preceded by the attribute name without quotation marks. Many times, the square brackets Application can make the logic very simple.

3. Get the value of the currently selected item

$(".selector").val();
Copy after login

4. Get the text of the currently selected item

$(".selector").find("option:selected").text();
Copy after login

A colon is used here. Mastering its usage and drawing inferences about other cases will also make the code concise.

Many times the cascade of select is used, that is, the value of the second select changes with the value selected by the first select. This is in jquery is very simple.

For example:

$(".selector1").change(function(){
// 先清空第二个
$(".selector2").empty();
// 实际的应用中,这里的option一般都是用循环生成多个了
var option = $("<option>").val(1).text("pxx");
$(".selector2").append(option);
});
Copy after login

jQuery gets the Text and Value selected by Select:

Syntax explanation:

1. $("#select_id").change(function(){//code...}); //Add an event for Select, which is triggered when one of the items is selected

2. var checkText=$("#select_id").find("option:selected").text(); //Get the Text selected by Select

3. var checkValue=$("#select_id").val(); //Get the Value selected by Select

4. var checkIndex=$("#select_id ").get (0).selectedIndex; //Get the index value selected by Select

5. var maxIndex=$("#select_id option:last").attr("index"); //Get the maximum index value of Select

jQuery sets the Text and Value selected by Select:

Syntax explanation:

1. $("#select_id ").get(0).selectedIndex=1; //Set the item with Select index value 1 to select

2. $("#select_id ").val(4); // Set the Select value to 4 to select the item

3. $("#select_id option[text='jQuery']").attr("selected", true) ; //Set the Text value of Select to the jQuery item selection

jQuery adds/delete the Option item of Select:

Syntax explanation:

1. $("#select_id").append("<option value='Value'>Text</option>"); //Append an Option (drop-down item) to Select

2. $("#select_id").prepend("<option value='0'>Please select</option>"); //Insert for Select An Option (the first position)

3. $("#select_id option:last").remove(); //Delete the Option with the largest index value in Select (the last one)

4. $("#select_id option[index='0']").remove(); //Delete the Option (first) with index value 0 in Select

5. $("#select_id option[value='3']").remove(); //Delete the Option with Value='3' in Select

5. $("#select_id option[text='4']").remove(); //Delete the Option with Text='4' in Select

jquery radio Value, checkbox value, select value, radio selected, checkbox selected, select selected, and related

Get the value of a group of radio selected items

var item = $(&#39;input[name=items][checked]&#39;).val();
Copy after login

Get the select selected item The text

var item = $("select[name=items] option[selected]").text();
Copy after login

The second element of the select drop-down box is the currently selected value

$(&#39;#select_id&#39;)[0].selectedIndex = 1;
Copy after login

The second element of the radio radio selection group is the currently selected value

$(&#39;input[name=items]&#39;).get(1).checked = true;
Copy after login

Get the value :

  • Text box, text area: $("#txt").attr("value");

  • Multiple selection box checkbox: $("#checkbox_id").attr("value");

  • Radio selection group radio: $( "input[type=radio][checked]").val();

  • Drop-down box select: $('#sel').val() ;

Control form elements:

Text box, text area: $("#txt").attr("value",''); //Clear the content

$("#txt").attr("value",'11');//Fill the content

Multiple selection box checkbox: $("#chk1" ).attr("checked",'');//Unchecked

$("#chk2").attr("checked",true);//Checked

if($("#chk1").attr('checked')==undefined) //Judge whether it has been checked

Radio group radio: $("input[type=radio]"). attr("checked",'2');//Set the item with value=2 as the currently selected item

Drop-down box select: $("#sel").attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item

$("< ;option value='1'>1111").appendTo("#sel")//Add the option of the drop-down box

$("#sel").empty(); //Clear the drop-down box

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of How to operate select with jquery. For more information, please follow other related articles on the PHP Chinese website!

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