JavaScript Radio按钮和事件处理

时间:2015-01-09 11:58:25

标签: javascript html forms events

我正在尝试设置一个带问题的互动小册子。

我在一个表格中设置了一个示例问题,该表格中有一个用于选择正确答案的单选按钮。

然而,当我运行脚本警报框(测试)时,我已立即运行设置并且脚本为空。

确切错误

Uncaught TypeError: Cannot set property 'onclick' of 
nullinteractive.js:34 mainQuestionOneinteractive.js:38 
(anonymous function)

我的代码

HTML

    <section class="question-one">
        <h4>Question 1</h4>
        <p>
            Which <stong>one</stong> of the following statements best describes the function of bookkeeping?
        </p>
        <table border="1">
            <tbody>
                <tr>
                    <td>
                        A form of internal accounting providing finanical information at a time and in a format which makes it useable by management for the purpose of planning and controlling a business
                    </td>
                    <td>
                        <input type="radio" name="question-one" id="question-one"/>
                    </td>
                </tr>
                    <td>
                        The mechanistic system of processing and recording the day-to-day financial transactions of a business
                    </td>
                    <td>
                        <input type="radio" name="question-one" id="question-two"  />
                    </td>
                </tr>
                </tr>
                    <td>
                        The mechanistic system of processing and recording the day-to-day financial transactions of a business
                    </td>
                    <td>
                        <input type="radio" name="question-one" id="question-three" />
                    </td>
                </tr>
            </tbody>
        </table>
    </section>

的JavaScript

/*--------------------------------------------
QUESTION ONE - LOGIC
----------------------------------------------*/
function mainQuestionOne() {

    var questionOne;
    var questionTwo;
    var questionThree;

    questionOne = document.getElementById('question-one');
    questionTwo = document.getElementById('question-two');
    questionThree = document.getElementById('question-three');

    questionOne.onclick = alert('test');
    questionTwo.onclick = alert('test2');
    questionThree.onclick = alert('test3');
}
mainQuestionOne();

2 个答案:

答案 0 :(得分:0)

您可以使用alert()包裹function() {...}

function mainQuestionOne() {
    var questionOne = document.getElementById('question-one');
    questionOne.onclick = function() { alert('test'); }
}
mainQuestionOne();

JSFiddle Link

答案 1 :(得分:0)

试试此代码

        问题1         

            哪一个             一             以下陈述最能说明记账的功能吗?         

                                                           一种内部会计形式,一次提供财务信息,其格式使管理层可用于规划和控制业务                                                                                                                                               处理和记录企业日常金融交易的机制系统                                                                                                                                               处理和记录企业日常金融交易的机制系统                                                                                                                                    

<script type="text/javascript">

    function que1() {
        alert('test1');
    }
    function que2() {
        alert('test2');
    }
    function que3() {
        alert('test3');
    }

</script>

通话功能&#39; onclick&#39;单选按钮事件