Javascript 表单输入检索
为什么在包含文本框和提交按钮的表单中,我可以提醒用户在文本框中输入的内容,但不能打印在页面上吗?我做错了什么?
这是代码
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它确实有效。文本框中的值将打印在页面上。
但是:
\\
在 Javascript 中没有任何意义。注释以//
开头。这很可能是您看不到正在写入的值的原因document.write
用其参数替换 HTML 页面中的任何内容。 (如果在文档加载后调用)。因此,除非您想学习 Javascript,否则这不是一个好主意。实际上,即使在学习 Javascript 时使用它也不是一个好主意,除非您想了解
document.write
的工作原理。有灵活(且更好)的方法来操作页面内容,从简单的 开始getElementById 到 复杂 DOM操纵
It does work. The value in the textbox is printed on the page.
BUT:
\\
do not mean anything in Javascript. Comments begin with//
. This is most likely the reason why you are not seeing the value being writtendocument.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
在页面加载/解析后使用 document.write() 不是一个好主意。此时,它将用新内容覆盖页面 HTML。 document.write() 通常在页面加载时使用,以便在页面加载时将特定点的内容插入到页面中。
如果你想将值放入页面上的某个项目中,那么你需要为此使用适当的 DOM 方法,将值放入输入字段中,在 div 上设置innerHTML,等等......
你可以阅读有关文档。写在这里:https://developer.mozilla.org/en/document.write。
以下是从字段中获取值并将其放入页面上的另一个对象中而不使用 document.write() 的示例: http://jsfiddle.net/jfriend00/dU8Sr/。
HTML:
JavaScript:
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:
Javascript: