SoFunction
Updated on 2025-04-05

Javascript operation select control code example

This article mainly introduces Javascript operation select control code examples. The example code is introduced in this article in detail, which has certain reference value for everyone's learning or work. Friends who need it can refer to it.

Add, modify, delete, select, clear, judge existence, etc.

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<;i++)
  {
   if([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 the item already exists");
  }
  else
  {
    var varitem = new option(objitemtext,objitemvalue);
    //   [] = varitem;
    (varitem);
    alert("Successfully Joined");
  }  
}

3. Delete an Item from the select option

function jsremoveitemfromselect(objselect,objitemvalue)
{
  //Judge whether it exists  if(jsselectisexititem(objselect,objitemvalue))
  {
   for(var i=0;i&lt;;i++)
   {
    if([i].value == objitemvalue)
    {
      (i);
     break;
    }
   } 
   alert("Successfully deleted");   
  }
  else
  {
     alert("This item does not exist in this select");
  }  
}

4. Delete the selected item in select

function jsRemoveSelectedItemFromSelect(objSelect){ 
   var length =  - 1;   
  for(var i = length; i >= 0; i—){      
    if(objSelect[i].selected == true){  
     [i] = null;  
   }
  }
}

5. Modify the text of value=”paraValue” in the select option to “paraText”

function jsupdateitemtoselect(objselect,objitemtext,objitemvalue)
{
  //Judge whether it exists  if(jsselectisexititem(objselect,objitemvalue))
  {
   for(var i=0;i&lt;;i++)
   {
     if([i].value == objitemvalue)
     {
    [i].text = objitemtext;
    break;
     }
   } 
   alert("Successfully modified");   
  }
  else
  {
    alert("This item does not exist in this select");
  }  
}

6. Set the first Item of text=”paraText” in select as selected

function jsselectitembyvalue(objselect,objitemtext)
{  
  //Judge whether it exists  var isexit = false;
  for(var i=0;i&lt;;i++)
  {
   if([i].text == objitemtext)
   {
     [i].selected = true;
     isexit = true;
    break;
   }
  }   
  //Show results  if(isexit)
  { 
   alert("Successfully selected");   
  }
  else
  {
    alert("This item does not exist in this select");
  }  
}

7. Set the Item of value=”paraValue” in select as selected

 = objItemValue;

8. Get the value of the currently selected item

var currSelectValue = ;

9. Get the text of the currently selected item

var currselecttext = [].text;

10. Get the index of the currently selected item

var currSelectIndex = ;

11. Clear the selected item

 =0;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.