我试图接受用户输入,将它变成一个JS变量,并在html中的多个位置使用它。在整个页面中使用JS变量(从HTML表单)
我已经在用户输入输入后立即包含了一个警报函数并且可以工作,但是我无法在底部显示该变量。
下面的代码:
<body>
<div class="hero-unit">
<h1> Title </h1>
<p> This form description </p>
<form class="well" name="formInput" action= "#">
<label>Input</label>
<input Id="txtvarInput" class="span3" style="margin: 0pt auto;" type="text" placeholder="AAA, BBB, CCC..." data-provide="typeahead" data-items="10" data-source="["AAA","BBB","CCC","DDD","EEE","FFF","GGG","HHH","III","JJJ","KKK","LLL"]"/>
</label>
<div class="form-actions" "span3">
<input name="submit" type="submit" class="btn" value="Select" onclick="alert('you chose ' + theInput.value)"/>
<script language="javascript" type="text/javascript">
var theInput = document.getElementById('txtvarInput');
</script>
</div>
</form>
</div>
<div class="page-header">
<h1>Input:
<script language="javascript" type="text/javascript">
document.write(theInput.value);
</script>
</h1>
</div>
您应避免与HTML – 2012-04-14 17:52:23
你就不能跳过此,并直接调用元素编写的代码内联? 'document.getElementById('txtvarInput').value'? – Kamil 2012-04-14 17:52:47