2015-12-02 73 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()'有關。確保你的請求是正確的。 –