Home > Web Front-end > JS Tutorial > body text

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

黄舟
Release: 2017-07-26 09:57:53
Original
1347 people have browsed it

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;    
}
Copy after login

// 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("成功加入");   
  }    
}
Copy after login

// 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中 不存在该项");    
  }    
}
Copy after login

// 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;  
    }  
  }  
}
Copy after login

// 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中 不存在该项");    
  }    
}
Copy after login

// 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中 不存在该项");    
  }    
}
Copy after login

// 7. Set the Item with value="paraValue" in select to be selected


document.all.objSelect.value = objItemValue;
Copy after login

// 8. Get the value of the currently selected item of select


var currSelectValue = document.all.objSelect.value;
Copy after login

// 9. Get the text of the currently selected item of select


var currSelectText = document.all.objSelect.options[document.all.objSelect.selectedIndex].text;
Copy after login

// 10. Get the Index of the currently selected item of select


var currSelectIndex = document.all.objSelect.selectedIndex;
Copy after login

// 11. Clear the selected items


document.all.objSelect.options.length =0;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!