单选按钮的javascript变量分配 [英] javascript variable assignment from radio button

查看:74
本文介绍了单选按钮的javascript变量分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

i have this partial code

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


the problem is, i want to put the value of the radio button that has been/ will be checked by the user to my array "sagots[0]" but i couldn't. please tell me how to do that

推荐答案

可能要提到的几点.
1)您只能使用一次特定的ID-它们应具有唯一性
2)用于同一组中的无线电元素的正确属性是名称(不是id)
3)document.getElementById(idStr)需要一个字符串,您已经给它提供了一个变量(不包含字符串)

也就是说,您不妨检查以下内容:

Javascript
A few points that should probably be mentioned.
1) You can only use a particular id once - they''re intended to be unique
2) The correct attribute to use for radio elements in the same group is name (not id)
3) document.getElementById(idStr) needs a string, you''ve given it a variable (that doesn''t hold a string)

That said, you may wish to examine the following:

Javascript
var choice1Radios = document.getElementsByName('choice1');
    var i, n = choice1Radios.length;
    for (i=0; i<n; i++)
    {
        if (choice1Radios[i].checked)
        {
            document.getElementById('outputDiv').innerHTML = 'Radio btn with value of (' + choice1Radios[i].value +') selected currently';
            i = n;
        }
    }



HTML



HTML

<div id='outputDiv'></div>
    <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>


这篇关于单选按钮的javascript变量分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆