JavaScript的选项交互(JavaScript Interacting with option

2019-10-29 21:57发布

我现在有一个下拉列表...完成空白的句子了。

我的问题是遗漏的类型错误:无法读取空的特性“选项”

这里是我的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();

任何人都可以帮我吗?

谢谢

Answer 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 } 
 <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> 



文章来源: JavaScript Interacting with options