Home  >  Article  >  Web Front-end  >  How to modify the selected value of select in jquery

How to modify the selected value of select in jquery

青灯夜游
青灯夜游Original
2022-09-07 17:56:205625browse

Modification steps: 1. Use the attribute selector to select the original selected value, and use the attr() function to remove the selected state, the syntax is "$("[selected]").attr("selected", false) ;"; 2. Obtain the specified option element through the id attribute value, and use the attr() function to set the selected state of the element. The syntax is "$("#id value").attr("selected", "selected");" .

How to modify the selected value of select in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer. The

select element creates a single-select or multiple-select menu. The

option element defines an option (an entry) in a drop-down list.

The browser displays the content in the

How to modify the select value in jquery

In jquery, you can modify the select value by adding the selected attribute to the specified option element.

Attribute Value Description
selected selected Specifies that the option appears selected (when first displayed in the list).

Implementation steps:

Step 1. Use the attribute selector to select the original selected value and use attr() The function removes the selected state

$("[selected]").attr("selected", false);

Step 2. Obtain the specified option element through the id attribute value, and use the attr() function to set the selected state for the element

$("#id值").attr("selected", "selected");

Implementation example code



	
		
		
		
	
	
		
		
	

How to modify the selected value of select in jquery

##Description:

The attr() method can set the attributes and values ​​of the selected element.

Syntax for setting a single property

$(selector).attr(attribute,value)

ParametersDescriptionSpecifies the name of the attribute. Specifies the value of the attribute.
attribute
value

Set multiple attribute/value pairs

Set more than one attribute and value for the selected element.

$(selector).attr({attribute:value, attribute:value ...})

ParameterDescriptionSpecify one or more attribute/value pairs.
attribute:value
[Recommended learning:

jQuery video tutorial, web front-end video

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

Statement:
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