1
當我提交我的表單時,會出現一個空白頁。我想留在同一頁面上。爲什麼我提交表單時出現空白頁?
HTML:
<form id='credentials'>
<div class="field">
<label for="name">Username:</label>
<input type="text" id="name" name="username" />
</div>
<div class="field">
<button type="submit">Send</button>
</div>
</form>
的JavaScript:
if (localStorage) { // checks for localStorage support
// add event listener for form submission
document.getElementById('credentials').addEventListener('submit', function() {
// get value of username field
var username = document.getElementById('username').value;
console.log('submitted')
// save name in localStorage
localStorage.setItem('name', username);
});
} else {
console.log('no localStorage');
}
'返回false;'在提交事件監聽器應該做的竅門 –
你似乎沒有一個元素與id用戶名' –
除了錯字,不。你可以設置一個測試用例,在http://jsfiddle.net上說? (我認爲現在也有內置支持,但說實話,我不知道它是如何工作的) –