从单选按钮分配javascript变量

时间:2013-09-29 19:21:05

标签: javascript html variables button radio

我有我的老师要我们的部分代码。我有3个数组,1个用于问题,1个用于答案,1个用于sagots。然后会出现问题,用户必须单击以回答第一个问题。我想将sagots[0]的值设为已单击的单选按钮的值。但我不能。我尝试使用alert()函数来确定它,但它似乎没有分配单选按钮的值

    <script>
        var question = [
            '1. In the equation, ans = (35+15) / 2 * 7 - 51 + (100+2), ans final value is?',
            '2. In PHP, what operator is the equal (=) sign?',
            '3. An electric train travelled northward by 3pm, then by 5pm, it travelled westward. What was the direction of its smoke by 5pm?',
            '4. What was the most expensive dog breed ever sold for 1.5 million dollars??',
            '5. An asteroid, (1998 KM41), was named after this Filipino teacher by the Massachusetts Institute of Technology, Lincoln Laboratory, USA. Who is she?',
            '6. Who was the first president of United States of America after Abraham Lincoln?',
            '7. This is also popularly known as crux ansata, what is it?',
            '8. What is the highest-grossing animated film worldwide?',
            '9. Who is this WWE superstar that always chant YES! YES! YES!?',
            '10. Who was the president of the Philippines who got the lowest vote ever recorded?'
        ];

        <!--alert(question[0]);-->          

    </script>

    <script>
        var sagots = [
            '1',
            '2',
            '3',
            '4',
            '5',
            '6',
            '7',
            '8',
            '9',
            '10'
        ];



    </script>

    <script>
        var ans=[
                '226',
                'Assignment',
                'None',
                'Red Tibetan mastiff',
                'Dr. Josette Biyo',
                'Andrew Johnson',
                'Ankh',
                'Toy Story 3',
                'Daniel Bryan',
                'Fidel Ramos'               
            ];
    </script>



    <script src="js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
    <!--[if lt IE 7]>
        <p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
    <![endif]-->

    <div id = "main-wrapper">
        <h1>Examination</h1>

        <div id = "questionone">
            <script>
                document.write(question[0]);
            </script>
            <br />
                <label><input type="radio" id="choice1" value="206"  checked="checked" />a. 206</label>
                <label><input type="radio" id="choice1" value="256"  />b. 256</label>
                <label><input type="radio" id="choice1" value="226"  />c. 226</label>
            <br />

            <script>

                    var sagots = 'null';

                    x = radio.getElementById(choice1).innerHTML;


                alert(sagots[0]);
            </script>

        </div>

问题是,我想把用户已经/将要检查的单选按钮的值放到我的数组sagots[0]但是我不能。请告诉我该怎么做

1 个答案:

答案 0 :(得分:1)

使用name而不是id

<label><input type="radio" name="choice1" value="206"  checked="checked" />a. 206</label>
<label><input type="radio" name="choice1" value="256"  />b. 256</label>
<label><input type="radio" name="choice1" value="226"  />c. 226</label>

然后,您可以使用jquery和css选择器获取checked元素:

sagots[0] = $('input[type=radio]:checked').val()

最后,将该代码放入函数中并将其绑定到无线电的click事件:

$('input[type=radio]').click(function() {
    sagots[0] = $('input[type=radio]:checked').val()
});