将数据传递到新页面并写入

发布于 2025-01-02 08:40:51 字数 925 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你怎么敢 2025-01-09 08:40:51

如果您的表单具有名称,并且文本字段具有名称,则可以使用document.myForm.myTextbox.value

<form name='myForm'>
    <input type='text' name='myTextbox'>

另外,在 DOM 上执行操作之前,请确保您的页面实际上已完全加载 - 如果您的 page_preview.html 在文本字段加载之前运行 javascript,这将导致它始终为 nil

If your form has a name, and the text field has a name, you can use 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.

自我难过 2025-01-09 08:40:51

为什么不为 onclick 事件增加价值呢?我的意思是传递参数给你的函数。像这样的东西

<input type="button" onclick="mypopup(document.getElementById('p_name').value)" value="Show preview" />

在你的javascript函数中,它会像这样

function load(p_name)
{
  document.write("<p>" + Date() + "</p>");

  var myTextField = p_name;
  if(myTextField.value != "")
  alert("You entered: " + myTextField.value)
}

或者错误可能是在你的getElementById中。请在这部分添加 .value:

 var myTextField = document.getElementById('p_name');

像这样:

 var myTextField = document.getElementById('p_name').value;

我认为这可以解决它。希望有帮助。

why don't you add value on your onclick event? 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

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. Please add .value on this part:

 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.

葬シ愛 2025-01-09 08:40:51

document.write 会覆盖页面中的 html,因此在该行执行后 不再存在。尝试修改 div(或 spanp 等)的内容以进行输出。

HTML 示例:

<div id="messages"></div>

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.

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 a div (or span or p, etc.) for output instead.

Example HTML:

<div id="messages"></div>

Example 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.
小嗲 2025-01-09 08:40:51

尝试使用 jQuery

$('#p_name').val()

Try to use jQuery

$('#p_name').val()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文