我无法设置或检索 iframe 的隐藏输入字段信息。
在初始页面上,我单击“提交”按钮以激活 showBox 功能并成功传递名称、城市和状态三个参数。showBox 创建一个弹出框,其中 iframe 应位于框顶部并进行表单操作。我需要使用从 showBox 函数传递的值来设置三个隐藏输入字段的值。这是展示框功能的代码 -
function showBox(name, city, state)
{
var div = document.createElement('div');
div.style.zIndex = 1;
div.id = 'box';
div.style.position = (navigator.userAgent.indexOf('MSIE 6') > -1) ? 'absolute' : 'fixed';
div.style.top = '20px';
div.style.left = (width / 2) - (900 / 2) + 'px';
div.style.height = '500px';
div.style.width = '400px';
div.style.backgroundColor = 'white';
div.style.border = '2px solid silver';
div.style.padding = '20px';
document.body.appendChild(div);
alert("Name: " + name + ", City: " + city + ", State: " + state);
div.innerHTML = "<iframe id='infopage' src='" + 'info.html' + "' width='100%' height='95%'></iframe>";
// set value of the hidden input fields
var myname = $("#infopage #hiddenname").val(name);
var mycity = $("#infopage #hiddencity").val(city);
var mystate = $("#infopage #hiddenstate").val(state);
// check hidden input fields value - which come back blank so far
alert("Name from hidden field: " + $("#hiddenname").val() + ", City from hidden field: " + $("#hiddencity").val() + ", State from hidden field: " + $("#hiddenstate").val());
// create close window link
var a = document.createElement('a');
a.innerHTML = '<br /><br />Close window';
a.href = 'javascript:void(0)';
a.onclick = function()
{
document.body.removeChild(document.getElementById('box'));
};
div.appendChild(a);
}
即使参数值已成功传递给 showBox 函数,但有些我无法使用参数成功设置隐藏输入字段值。
同时,这里是 info.html 的代码 -
<!DOCTYPE html>
<html>
<head>
<title>Sample person information</title>
<style type="text/css" title="currentStyle">
@import "css/core.css";
</style>
<script type="text/javascript" language="javascript" src="js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="js/jsonreport.js"></script>
<script type="text/javascript" src="js/info.js"></script>
</head>
<body>
<div id="container">
<div class="full_width big" id="testname">
<br />
<!-- (it may take a while to load large files) -->
</div>
<br />
<input type="text" name="hiddenname" id="hiddenname" value="" />
<input type="hidden" name="hiddencity" id="hiddencity" value="" />
<input type="hidden" name="hiddenstate" id="hiddenstate" value=""/>
<input id="submit" type="button" value=" Enter " />
<div id="AjaxDiv"></div>
</div>
</body>
</html>
单击“Enter”按钮时,将使用 iframe 中的隐藏字段值调用 ajax 函数。不知何故,我也很难从 iframe 中检索隐藏的输入字段值。