【问题标题】:JavaScript Interacting with optionsJavaScript 与选项交互
【发布时间】:2015-01-12 21:18:18
【问题描述】:

我目前有一个下拉列表...用来完成空白句子。

我遇到的问题是 Uncaught TypeError: Cannot read property 'options' of null

这是我的 HTML 的 sn-p

<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();

谁能帮帮我?

谢谢

【问题讨论】:

  • 你想要if (check.value ==
  • 你什么时候运行这个函数?它在事件处理程序中吗?
  • 当我把它放在一个 jsbin 中时它对我来说工作正常,你的 html 和脚本的顺序是什么?你是在加载 dom 后调用 checkAnswer() 吗?

标签: javascript html forms


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2013-05-19
    • 2010-10-02
    相关资源
    最近更新 更多