2016-02-26 81 views
1

这里是检索“id”是“token”的TAG的值的代码。该TAG存在于名为“script.php”的页面中。 JavaScript是工作,但document.write(val)不显示任何内容...从另一个页面检索值TAG

<!DOCTYPE html> 

<html> 

<head> 

    <title>Void</title> 

</head> 


<body> 

    <iframe src="script.php" name="myFrame" id="myFrame"></iframe> 

    <script> 
     document.write('<br/>'); 
     var doc = document.getElementById('myFrame'); 
     var val = doc.contentWindow.document.getElementById("token").value; 
     document.write(val); 
    </script> 


</body> 

</html> 

页面“script.php的”包含以下代码:

<form id="profile" action="" method="post" enctype="multipart/form-data"> 
     <div> 
     <label>Username:</label> 
     <input id="username" type="text" name="username" value="az"> 
     </div> 
     <br>   
     <div> 
     <label>Status:</label> 
     <input id="status" type="checkbox" name="status" disabled > 
     </div> 
     <br> 
     <input id="token" type="hidden" name="token" value="e4dea0c3a5a6246d98a6573f06ddfc97" /> 
     <button type="submit">Submit</button> 
</form> 

回答

0

只要你的iframe的域是和你一样

var iframe = document.getElementById('myFrame'); 
var innerDoc = iframe.contentDocument || iframe.contentWindow.document; 

alert(innerDoc.getElementById("token").value); 
+0

对不起,但是这段代码也不起作用... – mric750

+0

@ mric750你是否在控制台中看到任何错误? – gurvinder372

+0

我没有使用任何控制台,我通过在浏览器中加载它来执行该页面,如何查看我的错误? – mric750

-1

无论它们的范围如何,PHP中的几个预定义变量都是“superglobals”。出于这个原因,您可以从代码中的几乎任何地方访问它们。 t:var val = $GLOBALS['name_of your value'];

+0

你似乎混淆了PHP和JavaScript,以至于想出一些在两种语言中都不起作用的东西。 – Quentin

相关问题