我目前有一个下拉列表...完成空白句子。
我遇到的问题是Uncaught TypeError:无法读取null
的属性'options'以下是我的HTML片段
<section class="question-three">
<h4>Question 3</h4>
<ol>
<li>A partnership is a business entity
<select id="selectionOne">
<option value="#">Please Select...</option>
<option value="sole trader">sole trader</option>
<option value="members">members</option>
<option value="shareholders">shareholders</option>
<option value="run">run</option>
<option value="limited company">limited company</option>
<option value="owned">owned</option>
</select>
by two ore more people who carry a business collectively with a view to making a profit.
</li>
的JavaScript
var dropDownList = {
answers: ['sole trader', 'members', 'shareholders', 'run', 'limited company', 'owned'],
checkAnswer: function() {
var check = document.getElementById('selectionOne');
if(check.options == dropDownList.answers[5]) {
console.log('The answer is right!');
} else {
console.log('false')
}
}
};
dropDownList.checkAnswer();
有人可以帮帮我吗?
谢谢
答案 0 :(得分:1)
您没有访问value
。我们也不知道你何时触发事件。您需要在选择渲染后分配它:
var dropDownList = {
answers: ['sole trader', 'members', 'shareholders', 'run', 'limited company', 'owned'],
checkAnswer: function() {
var check = document.getElementById('selectionOne');
if(check.value == dropDownList.answers[5]) {
console.log('The answer is right!');
} else {
console.log('false')
}
}
};
window.onload=function() {
document.getElementById("checkIt").onclick=function() {
dropDownList.checkAnswer();
}
// or document.getElementById("selectionOne").onchange
}
&#13;
<section class="question-three">
<h4>Question 3</h4>
<ol>
<li>A partnership is a business entity
<select id="selectionOne">
<option value="#">Please Select...</option>
<option value="sole trader">sole trader</option>
<option value="members">members</option>
<option value="shareholders">shareholders</option>
<option value="run">run</option>
<option value="limited company">limited company</option>
<option value="owned">owned</option>
</select>
by two ore more people who carry a business collectively with a view to making a profit.
</li>
<button id="checkIt">Check</button>
&#13;