Js operation Select daqo of value set selected and so on

  • 2020-03-26 21:42:14
  • OfStack

Jquery operation select(value, set and selected)

Every time the operation select, always want to come out to turn over the data, as their own summary, after turning over here.

Such as < Select the class = "selector" > < / select>

1. Select the item with value set to PXX

$(" selector "). Val (" PXX ");

2. Set text to PXX

$(" selector "). The find (" option [text = 'PXX'] "). The attr (" selected ", true);

There is a use of brackets, in which the equal sign is preceded by the property name, without quotation marks. In many cases, the use of brackets makes the logic simple.

3. Get the value of the currently selected item

$(" selector "). Val ();

4. Gets the text of the currently selected item

$(" selector "). The find (" option: selected "). The text ();

The colon is used here. Knowing how to use it and drawing inferences from it will also make the code cleaner.

A lot of times you use a cascade of select, where the value of the second select changes as the value of the first select is selected. This is very simple in jquery.

Such as:
 
$(".selector1").change(function(){ 

//So let's clear the second one

$(".selector2").empty(); 

//In practice, this option usually generates multiple options in a loop

var option = $("<option>").val(1).text("pxx"); 

$(".selector2").append(option); 

}); 

Js operation Select daqo

Determines whether there is an Item with Value="paraValue" in the select option
Add an Item to the select option
Delete an Item from the select option
Deletes the item in the select select
Change the text of value="paraValue" in the select option to "paraText"
Set the first Item of text="paraText" in select to be selected
Set the Item value="paraValue" in select to be selected
Gets the value of the currently selected item of the select
Gets the text of the currently selected item of the select
Gets the Index of the currently selected item of the select
Clear the item of the select
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) { 
//To determine whether there is
if (jsSelectIsExitItem(objSelect, objItemValue)) { 
alert(" the Item the Value The value already exists "); 
} else { 
var varItem = new Option(objItemText, objItemValue); 
objSelect.options.add(varItem); 
alert(" Success to join "); 
} 
} 

//3. Delete an Item from the select option
function jsRemoveItemFromSelect(objSelect, objItemValue) { 
//To determine whether there is
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(" Deleted successfully "); 
} else { 
alert(" the select In the   There is no item "); 
} 
} 


//4. Delete the item selected in the select
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. Change the text of value="paraValue" in the select option to "paraText"
function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) { 
//To determine whether there is
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 changed "); 
} else { 
alert(" the select In the   There is no item "); 
} 
} 

//6. Set the first Item of text="paraText" in select to be selected
function jsSelectItemByValue(objSelect, objItemText) { 
//To determine whether there is
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; 
} 
} 
//Results Show that
if (isExit) { 
alert(" Success is selected "); 
} else { 
alert(" the select In the   There is no item "); 
} 
} 

//7. Set the Item of value="paraValue" in select as selected
document.all.objSelect.value = objItemValue; 

//8. Get the value of the currently selected item of the select
var currSelectValue = document.all.objSelect.value; 

//9. Gets the text of the currently selected item of the select
var currSelectText = document.all.objSelect.options[document.all.objSelect.selectedIndex].text; 

//10. Gets the Index of the currently selected item of the select
var currSelectIndex = document.all.objSelect.selectedIndex; 

//11. Clear the item of the select
document.all.objSelect.options.length = 0; 

Related articles: