我試圖接受用戶輸入,將它變成一個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