我正在尝试从表单中获取用户输入的信息以显示在页面的另一个部分中。
我的部分是页面左栏:
<section id="col1">
<h1>Blog Goes Here</h1>
</section>
然后这个表格:
<form name="blog" action="index.html">
<label for="bhead">Heading: </label>
<input type="text" id="bhead" />
<label for="bpost">Paragraph:</label>
<textarea id="bpost" rows="5" cols="25"></textarea>
<input type="submit" id="submit" value="Post" style="width:60px;" onclick="post()" />
</form>
<script type="text/javascript" src="scripts/blogPost.js"></script>
有一个对外部 js 文件的调用,其中包含以下内容:
function post()
{
var heading = document.blog.bhead.value;
var content = document.blog.bpost.value;
document.getElementById('col1').innerHTML = '<h1>'+$heading+'</h1> <p>'+$content+'</p>';
}
有没有办法让输入到表单中的文本显示在 id 为 col1 的部分中?
最佳答案
此行有错误:
document.getElementById('col1').innerHTML = '<h1>'+$heading+'</h1> <p>'+$content+'</p>';
应该是heading
和content
,而不是$heading
和$content
。
关于javascript 从表单发布到部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729467/