Home  >  Article  >  Web Front-end  >  How does javascript operate HTML drop-down list tags_javascript skills

How does javascript operate HTML drop-down list tags_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:44:021262browse

Let me first tell you 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 options

Delete the selected item in select

Modify the text of value="paraValue" in the select option to "paraText"

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

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

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 the first Item with text="paraText" in the select to be 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 the 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 content introduces the method of using javascript to operate html drop-down list tags. I hope everyone likes this article.

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