[英]Passing data to a new page and writing it
I am trying to pass data from text boxes using GetElementById . 我试图使用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. 在page_preview.html主体内部调用load方法。
This is the button which starts mypopup function: 这是启动mypopup功能的按钮:
<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. 除了document.getElementById('p_name')之外,每个东西都有效,它永远不会返回一个值,就像找不到它一样。
I am sure it can be done easily ( i am new to this ). 我相信它可以轻松完成(我是新手)。
If your form has a name
, and the text field has a name
, you can use document.myForm.myTextbox.value
. 如果表单具有
name
,并且文本字段具有name
,则可以使用document.myForm.myTextbox.value
。
<form name='myForm'>
<input type='text' name='myTextbox'>
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 be nil
. 此外,在对DOM执行操作之前,请确保您的页面实际上已完全加载 - 如果您的
page_preview.html
在文本字段加载之前运行javascript,那将导致它始终nil
。
why don't you add value on your onclick event? 为什么不为onclick事件增加价值? I mean pass parameters to your function.
我的意思是将参数传递给您的函数。 something like this
这样的事情
<input type="button" onclick="mypopup(document.getElementById('p_name').value)" value="Show preview" />
In your javascript function, it would be like this 在你的javascript函数中,它会是这样的
function load(p_name)
{
document.write("<p>" + Date() + "</p>");
var myTextField = p_name;
if(myTextField.value != "")
alert("You entered: " + myTextField.value)
}
Or maybe the error is in your getElementById. 或者错误发生在你的getElementById中。 Please add .value on this part:
请在此部分添加.value:
var myTextField = document.getElementById('p_name');
Make it like this: 像这样:
var myTextField = document.getElementById('p_name').value;
I think that would fix it. 我想这会解决它。 Hope that helps.
希望有所帮助。
尝试使用jQuery
$('#p_name').val()
document.write
overwrites the html in your page, so after that line executes <input type="text" name="name" id = "p_name"/>
no longer exists. document.write
覆盖页面中的html,因此在该行执行后<input type="text" name="name" id = "p_name"/>
不再存在。 Try modifying the contents of a div
(or span
or p
, etc.) for output instead. 尝试修改
div
(或span
或p
等)的内容以代替输出。
Example HTML: 示例HTML:
<div id="messages"></div>
Example JS: 示例JS:
var outputElement = document.getElementById;
var msgs = 'This is a better way to monitor output';
var htmlMsgs = '<p>This is a better way to monitor output</p>';
outputElement.innerText = msgs;
//outputElement.innerHTML = htmlMsgs; //for formatted output.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.