Javascript 表单输入检索

发布于 2024-11-19 03:55:21 字数 605 浏览 3 评论 0原文

为什么在包含文本框提交按钮的表单中,我可以提醒用户在文本框中输入的内容,但不能打印在页面上吗?我做错了什么?

这是代码

<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="submit" value="Join" onClick="serb(this.form)" />

<script type="text/javascript">
    function serb(form){
    var x = document.Serb.Name.value;   
    alert(x); \\this alerts
    document.write(x); \\this should print on page
    }
</script>

出于某种原因,警报工作正常,并准确显示用户在按“加入”后在用户名框中键入的内容。但是,它不会打印页面上的信息。这是为什么?

Why is it that in a form that contains a Text Box and a Submit Button, I can Alert what has been typed in the text box by the user, but can't print it on the page? What am I doing wrong?

Here's the code

<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="submit" value="Join" onClick="serb(this.form)" />

<script type="text/javascript">
    function serb(form){
    var x = document.Serb.Name.value;   
    alert(x); \\this alerts
    document.write(x); \\this should print on page
    }
</script>

For some reason, the alert works fine and displays exactly what the user typed in the username box after pressing 'Join'. However, it won't print the information on the page. Why is that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冷心人i 2024-11-26 03:55:21

确实有效。文本框中的值将打印在页面上。

但是:

  1. \\ 在 Javascript 中没有任何意义。注释以 // 开头。这很可能是您看不到正在写入的值的原因

  2. document.write 用其参数替换 HTML 页面中的任何内容。 (如果在文档加载后调用)。因此,除非您想学习 Javascript,否则这不是一个好主意。

    实际上,即使在学习 Javascript 时使用它也不是一个好主意,除非您想了解 document.write 的工作原理。
    有灵活(且更好)的方法来操作页面内容,从简单的 开始getElementById复杂 DOM操纵

It does work. The value in the textbox is printed on the page.

BUT:

  1. \\ do not mean anything in Javascript. Comments begin with //. This is most likely the reason why you are not seeing the value being written

  2. document.write replaces whatever is in the HTML page with its argument. (If it is called after the document is loaded). So unless you are trying to learn Javascript this is not a very good idea.

    Actually it is not a very good idea to use it even when learning Javascript, unless you are trying to learn how document.write works.
    There are flexible (and better) ways to manipulate the content of a page, starting from the humble getElementById to complex DOM manipulation

葮薆情 2024-11-26 03:55:21

在页面加载/解析后使用 document.write() 不是一个好主意。此时,它将用新内容覆盖页面 HTML。 document.write() 通常在页面加载时使用,以便在页面加载时将特定点的内容插入到页面中。

如果你想将值放入页面上的某个项目中,那么你需要为此使用适当的 DOM 方法,将值放入输入字段中,在 div 上设置innerHTML,等等......

你可以阅读有关文档。写在这里:https://developer.mozilla.org/en/document.write

以下是从字段中获取值并将其放入页面上的另一个对象中而不使用 document.write() 的示例: http://jsfiddle.net/jfriend00/dU8Sr/

HTML:

<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="button" value="Join" onClick="serb(this.form)" />
</form>
<br>
<br>Output: <span id="output"></span>

JavaScript:

function serb(form) {
    var x = document.Serb.Name.value;   
    document.getElementById("output").innerHTML = x;
}

It is not a good idea to use document.write() after the page has been loaded/parsed. At that point, it will overwrite the page HTML with new content. document.write() is generally used while the page is being loaded to insert content at a particular point into the page as it's being loaded.

If you want to put the value into some item on the page, then you need to use appropriate DOM methods for that, putting the value into an input field, setting the innerHTML on a div, etc...

You can read about document.write here: https://developer.mozilla.org/en/document.write.

Here's an example of fetching the value from the field and putting it in another object on the page without using document.write(): http://jsfiddle.net/jfriend00/dU8Sr/.

HTML:

<form name="Serb" action="" method="get">
<input name="Name" type="text" size="15" maxlength="20" />
<input name="Join" type="button" value="Join" onClick="serb(this.form)" />
</form>
<br>
<br>Output: <span id="output"></span>

Javascript:

function serb(form) {
    var x = document.Serb.Name.value;   
    document.getElementById("output").innerHTML = x;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文