2015-12-02 78 views
0

我正在使用SSO将html表单文章重定向到网站。用户名直接从注册表中获取。但是,我不确定为什么该值无法插入到表单值中。有人可以请指教。非常感激。如何从函数向HTML表单插入返回变量

<html> 
<head> 
<body> 
<script> 

var username; 
var test; 

function submitForm() { 
    document.forms["ciqForm"].submit(); 
} 

function getUsername() { 
    var WshShell = new ActiveXObject("WScript.Shell"); 
    username = WshShell.RegRead("HKEY_CURRENT_USER\\Software\\Plugin\\username"); 
    // username = "[email protected]" 
    return username; 
} 


// document.getElementbyName('extRedirUserName').value=getUsername(); 
document.forms['myForm'].elements['extRedirUserName'].value=getUsername(); 

</script> 
<body onload="submitForm()"> 
    <h1>Redirecting.</h1> 
    <form id="myForm" name="myForm" method="POST" action="https://www.somewebsite.com"> 
     <input type="hidden" id="hello" name="extRedirUserName" value="" /> 
     <input type="hidden" name="extRedirPassword" value= "password" /> 
    </form> 
</body> 
</head> 

`

+0

您提交上负载的形式,'<身体的onload = “submitForm()”>' – Ramanlfc

回答

0

也许是因为你没有使用正确的形式ID /名称?

document.forms["ciqForm"].submit(); 

但这里

form id="myForm" name="myForm" method="POST" 
0

DOM是当您正在运行 document.forms [ 'myForm的']的元素[ 'extRedirUserName']值= getUsername()尚未完全加载。;

尝试添加它作为submitForm函数的第一行(在文档已经执行后执行)。 我假设你已经检查过使用这种方法接收用户名。

0

随着一些变化:

 var username; 
     var test; 

     function submitForm() { 
      document.getElementById("myForm").submit();//you can change this with your code 
     } 

     function getUsername() { 
      var WshShell = new ActiveXObject("WScript.Shell"); 
      username = WshShell.RegRead("HKEY_CURRENT_USER\\Software\\Plugin\\username"); 
      // username = "[email protected]" 
      return username; 
     } 


     // document.getElementbyName('extRedirUserName').value=getUsername(); 
     document.forms['myForm'].elements['extRedirUserName'].value=getUsername(); 

    submitForm();//call here 
     </script> 
     <body > 
      <h1>Redirecting.</h1> 
      <form id="myForm" name="myForm" method="POST" action="https://www.somewebsite.com"> 
       <input type="hidden" id="hello" name="extRedirUserName" value="" /> 
       <input type="hidden" name="extRedirPassword" value= "password" /> 
      </form> 
     </body> 
     </head> 
+0

由于阿南德。但是,我尝试了所做的更改,但出于某种原因,返回的用户名仍然显示为“null”值。你知道我应该如何修改代码才能在获取用户名之前完全加载DOM? –

+0

我相信这与'getUsername()'有关。确保你的请求是正确的。 –