【问题标题】:Add the sum of from the value select of radiobutton从单选按钮的值选择中添加总和
【发布时间】:2012-12-09 20:37:10
【问题描述】:

例如,单选按钮一 = 值 1,单选按钮二 = 值 2。

这是我的代码:

脚本文件:

<script type="text/javascript">


$(document).ready(function () {

    $("div[data-role='footer']").prepend('<a href="#" data-rel="back" data-icon="back">Back</a>');

    $(".Next").click(function () {

        $.mobile.changePage("#" + $("#Answer").val());
    });

    $("input[type=radio]").click(function () {
        var answer = $(this).val();
        $("#Answer").val(answer);

    });
    $('.Answer').live("click", function () {
        var NextQuestionID = $(this).attr('NextQuestionId');
        if (NextQuestionID == '') {
            location.href = "/Surveys/Index";
        }
        $("#survey").load('/Questions/GetQuestion', { Id: NextQuestionID }, function () {
            $('#answerInput').textinput();
            $(".Answer").button();
        });

    });
});

这是我的标记:

<input type="radio" name="Answer" id="radio-choice-1" value="Question2" />



     <input id="Answer" class="Answer" type="hidden" value="first" />
        <div class="innerspacer">
        <a href="" class="Next" data-theme="d" data-role="button">Next</a> 
</div>

如何将单选按钮分配为从 1 到 4 的值并总结所有问题的值?

【问题讨论】:

  • 你至少应该提供足够的 HTML 让人们知道你想要什么,以及更好的解释,因为它与实际的 html 相关。还要省略不相关的代码,例如将元素附加到与您的问题无关的页脚

标签: javascript jquery jquery-selectors jquery-events


【解决方案1】:

您的问题有很多内容,不清楚您想要什么。我猜测并假设您有 5 个单选按钮,并且您希望第 5 个单选按钮值是其他 4 个值的总和。对吗?

这是一个这样做的例子:jsfiddle

HTML:

<div id="container">
    <label>
        <input type="radio" name="something" value="1">
        A?
    </label>
    <label>
        <input type="radio" name="something" value="3">
        B?
    </label>
    <label>
        <input type="radio" name="something" value="5">
        C?
    </label>
    <label>
        <input type="radio" name="something" value="">
        All?
    </label>
</div>

JavaScript:

$(document).ready(function() {
    var choices = $('input[name="something"]');
    var total = 0;
    choices.each(function() {
        var choice = $(this);
        var value = parseInt(choice.val(), 10);
        if (!isNaN(value)) {
            total += value;
        }
    });
    choices.filter(':last').val(total);
});

您需要根据您的 HTML 进行调整。

【讨论】:

    猜你喜欢
    • 2012-02-14
    • 2013-10-27
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多