2012-12-09 66 views
0

例如,一个单选按钮值= 1的值增加的总和,单选按钮2 =值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的值并总结所有问题的值?

+0

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

回答

0

在你的问题中有很多事情发生,你不清楚你想要什么。我在猜测,假设你有一个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。