源码:<!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=gb2312" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="bd.css" />
</head>
<script language="javascript">
var opttext= new Array(100);
var optvalue=new Array(100);function change(object){
opt=object.options[object.selectedIndex];
alert(opt.value+" : "+opt.text);
}
for(i=0;i<opttext.length;i++)
{
opttext[i]=i;
optvalue[i]=i;
}function option(){
    var opt;
    var start;
    var end;   
    for(i=1;i<opttext.length;i++)
    {    opt=new Option();
        opt.text=opttext[i];
        opt.value=optvalue[i];
        selShow.options.add(opt);

    }
    end=new Date();
}</script>
<body>
<form action="" method="post"><select name="selShow" id="selShow" ><option value="0">0</option></select>
</form></body>
</html>
去掉form标签,则可以执行js函数,加上form标签,则不执行JS函数,在线求高手指教。谢谢了。

解决方案 »

  1.   

    我是要在页面初始化完毕后就执行JS函数。 开始我在BODY中加入 onLoad="option()" 如果没有form标签,一切正常, 如果加上form标签,则JS不管事了 真是纠结,百思不得其解,求高手解答。
      

  2.   

    document.getElementById("selShow").options.add(opt);
      

  3.   

    selShow.options.add(opt); 改一下 
     document.getElementById("selShow").options.add(opt);body onload="option()"
    或者将脚本放到select下面
      

  4.   

    selShow.options.add(opt);>>document.getElementById("selShow").options.add(opt);
      

  5.   

    <!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=gb2312" />
        <title>无标题文档</title>
        
    </head>
    <script language="javascript">
        var opttext = new Array(100);
        var optvalue = new Array(100);    function change(object) {
            opt = object.options[object.selectedIndex];
            alert(opt.value + " : " + opt.text);
        }
        for (i = 0; i < opttext.length; i++) {
            opttext[i] = i;
            optvalue[i] = i;
        }    function option() {
            var opt;
            var start;
            var end;
            for (i = 1; i < opttext.length; i++) {
                opt = new Option();
                opt.text = opttext[i];
                opt.value = optvalue[i];
                document.getElementById("selShow").options.add(opt);        }
            end = new Date();
        }</script>
    <body onload="option()">
       <form action="" method="post">
        <select name="selShow" id="selShow">
            <option value="0">0</option>
        </select>
        </form>
    </body>
    </html>
      

  6.   

    <!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=gb2312" />
    <title>无标题文档</title>
    </head>
    <body>
    <form action="" method="post">
    <select name="selShow" id="selShow" onchange="change(this)"><option value="0">0</option></select>
    </form>
    <script language="javascript">
    var opttext= new Array(100);
    var optvalue=new Array(100);
    function change(object)
    {
    opt=object.options[object.selectedIndex];
    alert(opt.value+" : "+opt.text);
    }
    for(i=0;i<opttext.length;i++)
    {
    opttext[i]=i;
    optvalue[i]=i;
    }
    function option()
    {
    var opt;
    var start;
    var end;   
    for(i=1;i<opttext.length;i++)

    opt=new Option();
    opt.text=opttext[i];
    opt.value=optvalue[i];
    document.getElementById('selShow').options.add(opt);
    }
    end=new Date();
    }
    window.onload = option();
    </script>
    </body>
    </html>
    经测试,这样是可行的。
    不知道你是不是想要这样的效果?
      

  7.   

    这个和form无关,刚试了一下。在chrome下可以正常执行。因为在chrome下selShow是可以直接获取到select对象。而在ff和IE下则会报selShow is not defined的错误。如楼上的,用document.getElementById来操作select是可以的。