select 多选 js设置默认值

javascript

17-2-17 8:37:50

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label for="select"></label>
  <select name="select" size="6" multiple="multiple" id="select">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>
</form>


<script type="text/javascript">

 var op=document.getElementById("select");
 for(var i=0;i<op.options.length;i++){
    if(op.options[i].text=="1" || op.options[i].text=="2" ){
op.options[i].selected=true;//在这里做
  }
 }
</script>
</body>
</html>