Home  >  Article  >  Web Front-end  >  Detailed explanation of examples of JavaScript operation html drop-down list tags

Detailed explanation of examples of JavaScript operation html drop-down list tags

黄舟
黄舟Original
2017-07-26 09:57:531252browse

Drop-down lists are often encountered in website front-end development. How to operate the html drop-down list tag. This article will explain in detail how to operate the html drop-down list tag with javascript. Friends who need it can refer to it.

Give it first Let’s talk about the general implementation idea. Please see below for the specific content.

Determine 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

Delete the selected item in the select

Modify the value="paraValue" in the select option text is "paraText"

Set the first Item of text="paraText" in the select to be selected

Set value=" in the select The Item of paraValue" is selected

Get the value of the currently selected item of 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) {    
  //判断是否存在    
  if (jsSelectIsExitItem(objSelect, objItemValue)) {    
    alert("该Item的Value值已经存在");    
  }else{    
    var varItem =new Option(objItemText, objItemValue);   
    objSelect.options.add(varItem);   
    alert("成功加入");   
  }    
}

// 3. Delete an Item from the select option


function jsRemoveItemFromSelect(objSelect, objItemValue) {    
  //判断是否存在    
  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("成功删除");    
  }else{    
    alert("该select中 不存在该项");    
  }    
}

// 4. Delete the selected item in 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. Modify the text of value="paraValue" in the select option to "paraText"


function jsUpdateItemToSelect(objSelect, objItemText, objItemValue) {    
  //判断是否存在    
  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("成功修改");    
  }else{    
    alert("该select中 不存在该项");    
  }    
}

// 6. Set text="paraText" in the select "The first Item is selected


function jsSelectItemByValue(objSelect, objItemText) {      
  //判断是否存在    
  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出结果    
  if (isExit) {    
    alert("成功选中");    
  }else{    
    alert("该select中 不存在该项");    
  }    
}

// 7. Set the Item with value="paraValue" in select to be 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 items


document.all.objSelect.options.length =0;

The above is the detailed content of Detailed explanation of examples of JavaScript operation html drop-down list tags. 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