将数据传递到新页面并写入
我正在尝试使用 GetElementById 从文本框中传递数据。
这是我的文本框:
<tr>
<td>Your name:</td>
<td> <input type="text" name="name" id = "p_name"/><br /></td>
</tr>
这些是我的函数:
function mypopup()
{
mywindow = window.open("Page_preview.html","width=200,height=200");
mywindow.moveTo(0, 0);
}
function load()
{
document.write("<p>" + Date() + "</p>");
var myTextField = document.getElementById('p_name');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
}
load 方法在 page_preview.html 正文中调用。
这是启动 mypopup 函数的按钮:
<input type="button" onclick="mypopup()" value="Show preview" />
除了 document.getElementById('p_name') 永远不会返回值,就像找不到它一样,一切正常。
我确信这可以轻松完成(我对此很陌生)。
I am trying to pass data from text boxes using GetElementById.
this is my text box:
<tr>
<td>Your name:</td>
<td> <input type="text" name="name" id = "p_name"/><br /></td>
</tr>
And these are my functions:
function mypopup()
{
mywindow = window.open("Page_preview.html","width=200,height=200");
mywindow.moveTo(0, 0);
}
function load()
{
document.write("<p>" + Date() + "</p>");
var myTextField = document.getElementById('p_name');
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
}
The load method is called inside the page_preview.html body.
This is the button which starts mypopup function:
<input type="button" onclick="mypopup()" value="Show preview" />
Every thing works except the document.getElementById('p_name') never returns a value, like it can't find it.
I am sure it can be done easily ( i am new to this ).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的表单具有
名称
,并且文本字段具有名称
,则可以使用document.myForm.myTextbox.value
。另外,在 DOM 上执行操作之前,请确保您的页面实际上已完全加载 - 如果您的
page_preview.html
在文本字段加载之前运行 javascript,这将导致它始终为nil
。If your form has a
name
, and the text field has aname
, you can usedocument.myForm.myTextbox.value
.Also, make sure your page has actually fully loaded before you perform actions on the DOM - if your
page_preview.html
runs the javascript BEFORE the text field loads, that will cause it to always benil
.为什么不为 onclick 事件增加价值呢?我的意思是传递参数给你的函数。像这样的东西
在你的javascript函数中,它会像这样
或者错误可能是在你的getElementById中。请在这部分添加 .value:
像这样:
我认为这可以解决它。希望有帮助。
why don't you add value on your onclick event? I mean pass parameters to your function. something like this
In your javascript function, it would be like this
Or maybe the error is in your getElementById. Please add .value on this part:
Make it like this:
I think that would fix it. Hope that helps.
document.write
会覆盖页面中的 html,因此在该行执行后不再存在。尝试修改
div
(或span
或p
等)的内容以进行输出。HTML 示例:
JS 示例:
document.write
overwrites the html in your page, so after that line executes<input type="text" name="name" id = "p_name"/>
no longer exists. Try modifying the contents of adiv
(orspan
orp
, etc.) for output instead.Example HTML:
Example JS:
尝试使用 jQuery
Try to use jQuery