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

How does javascript operate HTML drop-down list tags_javascript skills

WBOY
Release: 2016-05-16 15:44:02
Original
1310 people have browsed it

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

/// 7. Set the Item with value="paraValue" in the 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 content introduces the method of using javascript to operate html drop-down list tags. I hope everyone likes this article.

Related labels:
js
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