


Js operation Select encyclopedia (value, setting selection, etc.)_javascript skills
jquery operation select (value, setting selection)
Every time you operate select, you always have to go out and look up the information. Why not summarize it yourself, and I will look here in the future.
For example,
1. Set the item with value to pxx to select
$(".selector") .val("pxx");
2. Set text to pxx and select the item
$(".selector").find("option[text='pxx']") .attr("selected",true);
There is a usage of square brackets. The equal sign in the square brackets is preceded by the attribute name without quotation marks. Many times, the use of square brackets can make logic very simple.
3. Get the value of the currently selected item
$(".selector").val();
4. Get the text of the currently selected item
$(".selector").find("option:selected").text();
Colon is used here. Mastering its usage and applying inferences will also make the code simpler.
The cascade of selects is often used, that is, the value of the second select changes with the value selected by the first select. This is very simple in jquery.
For example:
$(". selector1").change(function(){
// Clear the second one first
$(".selector2").empty();
// Actual In applications, multiple options here are generally generated using loops
var option = $("
Js Operation Select Encyclopedia
Judge whether the select option exists Item with Value="paraValue"
Add an Item to the select option
Delete an Item from the select option
Delete the selected item in the select
Modify the text of value="paraValue" in the select option For "paraText"
Set the first Item with text="paraText" in the select to be selected
Set the Item with value="paraValue" in the select to be selected
Get the value of the currently selected item in the select
Get the text of the currently selected item of select
Get the Index of the currently selected item of select
Clear the selected item
js code
/// 1. Determine whether there is an Item with Value="paraValue" in the select option
function jsSelectIsExitItem(objSelect, objItemValue) {
var isExit = false;
for (var i = 0; i < objSelect.options.length; i ) {
if (objSelect.options[i].value == objItemValue) {
isExit = true;
break;
}
}
return isExit;
}
// 2. Add an Item to the select option
function jsAddItemToSelect(objSelect, objItemText, objItemValue) {
//Judge whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
alert("The Value of this Item already exists");
} else {
var varItem = new Option(objItemText, objItemValue);
objSelect.options.add(varItem);
alert("Successfully added");
}
}
// 3. Delete an Item from the select option
function jsRemoveItemFromSelect(objSelect, objItemValue) {
//Determine whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < objSelect.options.length; i ) {
if (objSelect.options[i].value == objItemValue) {
objSelect.options.remove(i);
break;
}
}
alert(" Successfully deleted");
} else {
alert("This item does not exist in this selection");
}
}
// 4. Delete selection The selected item in
function jsRemoveSelectedItemFromSelect(objSelect) {
var length = objSelect.options.length - 1;
for(var i = length; i >= 0; i--){
if(objSelect[i].selected == true){
objSelect.options[i] = null;
}
}
}
// 5. Modify select The text of value="paraValue" in the option is "paraText"
function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {
//Determine whether it exists
if (jsSelectIsExitItem(objSelect, objItemValue)) {
for (var i = 0; i < objSelect.options.length; i ) {
if (objSelect.options[i].value == objItemValue) {
objSelect.options[i].text = objItemText ;
break;
}
}
alert("Successfully modified");
} else {
alert("This item does not exist in the selection");
}
}
// 6. Set the first Item with text="paraText" in the select to be selected
function jsSelectItemByValue(objSelect, objItemText) {
//Judge whether it exists
var isExit = false;
for (var i = 0; i < objSelect.options.length; i ) {
if (objSelect.options[i].text == objItemText) {
objSelect.options[i].selected = true;
isExit = true;
break;
}
}
//Show the result
if (isExit) {
alert("Successfully selected");
} else {
alert("This item does not exist in the selection");
}
}
// 7. Set select The Item with value="paraValue" is selected
document.all.objSelect.value = objItemValue;
// 8. Get the value of the currently selected item of select
var currSelectValue = document.all .objSelect.value;
// 9. Get the text of the currently selected item of select
var currSelectText = document.all.objSelect.options[document.all.objSelect.selectedIndex].text;
// 10. Get the Index of the currently selected item of select
var currSelectIndex = document.all.objSelect.selectedIndex;
// 11. Clear the selected item
document.all .objSelect.options.length = 0;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...
