Home  >  Article  >  Web Front-end  >  JavaScript implements select and adds option_javascript tips

JavaScript implements select and adds option_javascript tips

WBOY
WBOYOriginal
2016-05-16 15:51:351291browse

JavaScript adds option to select

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>测试文件</title>
<script>
window.onload = function(){
  //创建select控件
  var _select = document.createElement("SELECT");
  
  //添加选项
  for(var i=1; i<=10; i++){
    var _option = new Option(i,i);
    _select.options.add(_option);
  }
  
  //选中值发生改变时的处理函数
  _select.onchange = function(){
    alert(_select.value); //取select控件被选中的值
  }
  
  document.body.appendChild(_select);
}
</script>
</head>

<body>
</body>
</html>

The above is the entire content of this article, I hope you all like it.

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