我想在div元素(id =「displayStoragevol2」)中顯示詳細信息。爲什麼下面的代碼不起作用顯示html元素的值時JavaScript代碼不起作用
<hr>
<h3>LocalStorage App Vol-2</h3>
<label>First Name: </label><input type="text" id="firstNamevol2">
<label>Last Name: </label><input type="text" id="lastNamevol2">
<button type="submit" id="fetchDetailsvol2">Submit</button>
<div id="displayStoragevol2"></div>
<hr>
<script>
/*LocalStorage Vol 2*/
document.getElementById('fetchDetailsvol2').addEventListener('click', fetchFunction);
var firstNamevol2 = document.getElementById('firstNamevol2').value;
var lastNamevol2 = document.getElementById('lastNamevol2').value;
var displayStoragevol2 = document.getElementById('displayStoragevol2');
function fetchFunction(){
displayStoragevol2.innerHTML = firstNamevol2 + lastNamevol2;
}
</script>
取裏面的功能價值和使用'類型= 「按鈕」' – Satpal
你應該[瞭解如何使用標籤元素正確](http://www.456bereastreet.com/archive/200711/use_the_label_element_to_make_your_html_forms_accessible/)。如果沒有for屬性或者表單控件,標籤就沒用了。 – Quentin