页面test.htm中的代码为:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<frameset rows="90,*" frameborder="no" border="0" framespacing="0">
<frame name="topFrame" src="test1.htm" frameborder="0" border="0" scrolling="auto">
<frame name=""mainFrame" src="http://xxx.com/login" frameborder="0" border="0" scrolling="auto">
</frameset>
</html>页面test1.htm中的代码为:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language=JavaScript>
window.onload=function(){
top.mainFrame.document.loginform.Username.value='用户名';
top.mainFrame.document.loginform.Password.value='密码';
top.mainFrame.document.loginform.submit();
}
</script>
</head>
<body>
</body>
</html>想通过在test1.htm中的javascript置值,提交另外一个页面中的表单,但是不起作用,另外一个框架中的页面http://xxx.com/login没有被响应提交,请教大家如何可以实现我说的这种效果?谢谢!

解决方案 »

  1.   

    也许是这的问题吧
    <frame name=""mainFrame"
    去掉个引号
      

  2.   

    同域下需要考虑一个问题。
    你的test1 在执行 onload的时候
    mainFrame的元素是否已经加载好了。
    如果那时没有加载好。怎么可能让你执行你想要执行的功能呢?
      

  3.   

    对于上面我说的那种问题的解决方案是
    在mainFrame
    中放一个全局变量 isLoadOver=false;
    onload事件中 isLoadOver=True;在test中
    循环调用,如果isLoadover为false就过一段时间再看一次。
    如果为true执行提交方法,然后停止计时器
      

  4.   

    谢谢楼上的各位,1、代码中笔误,多了个引号,但去掉后仍然没有效果的。2、我测试的时候打开页面后,在上面的框架中test1.htm右键选择刷新,仍然没有效果,而这个时候,test.htm早已加载完毕的。
      

  5.   

    调试一下
    top.mainFrame.document.loginform能取到么?
    在test1.html中
      

  6.   

    经过我的测试
    在主页面中获得主页面中的框架中页面的元素或者是变量函数
    方法是:
    var iframe = document.getElementById("mainframes");
    iframe.onload = function() {
    var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
    //alert(iframe.contentWindow.a);  //获得变量 不兼容
            //iframeDoc.getElementById('divId') //获得元素
    }在主页面中一个框架中获得两一个或者自己框架元素或变量方法是:在主页面中一个框架中的js代码
    alert(top.mainframe.a); //ie
      

  7.   

    iframe.html
    <!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>Untitled Document</title>
    <style type="text/css">
    body { margin:0; padding:0; }
    #divId { width:500px; height:500px; }</style>
    </head><body>
    <div id="divId">
    <iframe id="mainframe" name="mainframes" src="1.html" frameborder="0" width="100%" height="100%"></iframe>
    </div>
    <div id="diId">
    <iframe id="main" name="mains" src="3.html" frameborder="0" width="100%" height="100%"></iframe>
    </div>
    <script type="text/javascript">
    /*var iframe = document.getElementById("mainframes");
    iframe.onload = function() {
    var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
    //alert(iframe.contentWindow.a);  
    }*/
     
    </script>
    </body>
    </html>1.html
    <!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>Untitled Document</title>
    <script type="text/javascript">
    var a = 1;
    function fns() {
    return 2;
    }
    </script>
    </head><body>
    <div id="diid">sfsf</div>
    <div>sfsf</div>
    <script type="text/javascript">
    alert(top.mains.b); 
    </script>
    </body>
    </html>3.html
    <!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>Untitled Document</title>
    <script type="text/javascript">
    var b = 9;
    </script>
    </head><body>
    <div id="di">sfsfs</div>
    </body>
    </html>
      

  8.   

    js
    window.onload=function(){
        var iframe = document.getElementById("mainFrame");
    iframe.onload = function() {
        var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
        iframeDoc.loginform.Username.value='用户名';
        iframeDoc.loginform.Password.value='密码';
        iframeDoc.loginform.submit();
    }}
    html
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <frameset rows="90,*" frameborder="no" border="0" framespacing="0">
    <frame name="topFrame" src="test1.htm" frameborder="0" border="0" scrolling="auto">
    <frame name="mainFrame" id='mainFrame' src="http://xxx.com/login" frameborder="0" border="0" scrolling="auto">
    </frameset>
    </html>
      

  9.   

    感谢veryhunger,代码在IE10兼容模式下测试通过,但IE10普通模式下没有效果,而且mainFrame表单中只能是同域,跨域也无法达到效果,这个是IE安全设置的原因吗?通过设定受信任站点或其他办法是否可以实现跨域的效果?
      

  10.   

    <frame name="mainFrame" id='mainFrame'
    这儿加id了吗?