0

我是 javascript 新手,所以请善待。我的网站上有一种测验。我需要遍历选中的单选按钮并在用户单击提交时添加值。

将值相加后,必须出现一个小的弹出窗口来显示结果。如果单选按钮的总值在 0-10 之间显示消息 1 否则如果单选按钮的总值在 11-20 之间显示消息 2 否则显示消息 3

我已经搜索了互联网,但我只是找不到关于如何循环通过单选按钮并加起来的简单工作解释

我的 HTML 表单看起来像这样

你今年多大?
18-21
22-30
30-42
42

你长什么样?
我看起来像泰国
人 我是典型的外国人(白皮肤、金发、蓝眼睛)
我是女孩
我年纪大了

你的学历是多少
高中
大学文凭/证书
学士学位
硕士学位或更高

                  <strong>From which country are you?</strong><br />
                 <input type="radio" value="3" name="country" />Aus, NZ, SA, UK, US<br />
                 <input type="radio" value="0" name="country" />Other country from the once listed above<br />
                 <br />

                  <strong>Is English your naitive language</strong><br />
                 <input type="radio" value="2" name="country" />yes<br />
                 <input type="radio" value="0" name="country" />no<br />
                 <input type="radio" value="1" name="country" />no, but I am fluent<br />
                 <br />

                 <strong>Do you have any experience in teaching English in Thailand?</strong>
                 <br />
                 <input type="radio" value="0" name="experience" />none<br />
                 <input type="radio" value="1" name="experience" />0-1 year<br />
                 <input type="radio" value="3" name="experience" />1-2 years<br />
                 <input type="radio" value="4" name="experience" />2 years +<br />
                 <br />

                   <strong>What is your knowledge of Thai language and Thai culture</strong>
                 <br />
                 <input type="radio" value="1" name="culture" />I know nothing<br />
                 <input type="radio" value="2" name="culture" />I know my basics<br />
                 <input type="radio" value="3" name="culture" />I know slightly more than the basics<br />
                 <input type="radio" value="4" name="culture                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                \   " />My friends say I am Thai<br />
                 <br />

                    <strong>What is your knowledge of Thai language and Thai culture</strong>
                 <br />
                 <input type="radio" value="1" />I know nothing<br />
                 <input type="radio" value="2" />I know my basics<br />
                 <input type="radio" value="3" />I know slightly more than the basics<br />
                 <input type="radio" value="4" />My friends say I am Thai<br />
                 <br />

                    <strong>Do you have any visible tatoos or piercings?</strong>
                 <br />
                 <input type="radio" value="2" />Yes but it is not visible<br />
                 <input type="radio" value="4" />none<br />
                 <input type="radio" value="0" />Visible tatoo or piercing<br />
                 <br />

                 <strong>What gender are you</strong>
                 <br />
                 <input type="radio" value="1" />Male<br />
                 <input type="radio" value="4" />Female<br />
                 <br />
                 <input type="submit" onclick="showTotal" />


                 </form>
4

2 回答 2

1

这篇文章向您展示了如何获取单选按钮组的值:我们如何使用 DOM 访问单选按钮的值?

对你来说最简单的答案是:

function getRadioValue (theRadioGroup)
{
    for (var i = 0; i < document.getElementsByName(theRadioGroup).length; i++)
    {
        if (document.getElementsByName(theRadioGroup)[i].checked)
        {
                return document.getElementsByName(theRadioGroup)[i].value;
        }
    }
}

var country = parseInt(getRadioValue ('country'));
var experience = parseInt(getRadioValue ('experience'));

等等,您需要将它们全部添加并提醒它们

var total = country + experience; // add all the other variables here
alert(total);

希望有帮助。

于 2013-01-08T05:23:51.497 回答
-1

只需在 head 部分添加以下脚本,然后您将获得如下总价值。

<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script>
<script type="text/javascript">
function showTotal()
{
    total = 0;
    $("input[type=radio]:checked").each(function(){
        total += Number($(this).val());
    });
    alert(total);
}
</script>
于 2013-01-08T05:19:27.353 回答