2012-12-09 266 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。