[英]Event listener not responding - Javascript
我正在嘗試用純JavaScript編寫測驗,以從數組中加載新問題並在用戶單擊下一步時填充表格。 我遇到的一個看似簡單的問題是,當用戶單擊下一步按鈕時,無法使事件偵聽器正常工作。 在JSFiddle上,它可以工作,但對我而言不起作用,我也不知道為什么。 就像測試一樣,我會警告是否按下了按鈕。 我還測試了多種不同的方法,以檢查文檔是否已加載,然后再執行無濟於事。
HTML
<body>
<table class="center">
<tr>
<th id='question' colspan="2">question</th>
<td colspan="2"></td>
</tr>
<tr>
<td id="answer0">Choice 1</td>
<td><input id="radio0" type="radio" name=que_1 value="1" required/></td>
</tr>
<tr>
<td id="answer1">choice 2</td>
<td><input id="radio01" type="radio" name=que_1 value="1"/></td>
</tr>
<tr>
<td id="answer2">choice 3</td>
<td><input id="radio02" type="radio" name=que_1 value="1"/></td>
</tr>
<tr>
<td id="answer3">choice 4</td>
<td><input id="radio03" type="radio" name=que_1 value="1"/></td>
</tr>
</table>
<div>
<input type="button" id="next" value='Next'>
</div>
</body>
Javscript
document.getElementById("next").addEventListener("click", function() {
alert("works");
});
在此先感謝您。
確保在加載DOM之后綁定事件處理程序:
window.addEventListener('load', function() {
document.getElementById("next").addEventListener("click", function() {
alert("works");
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.