猿问

尝试通过选择“是”的 # 个答案来更新总列

每个人。


我的目标是添加“是”答案的总数,但我遇到了一些麻烦。我还想减去总列,但我还没有到那里,因为“是”的 # 没有正确显示。仅供参考,我正在调用函数 calculateSum()


任何帮助将不胜感激。非常感谢。


这是代码。


HTML:

-- Question

<li id="qid61915_1" value="11" style="margin: 20px 0px 30px 0px" ;="" required="true">Test Question


-- Yes

<div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_1" 

id="aid73380_1" class="currentStepAnswer" value="Yes">Yes</label></div>


-- No

<div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_1" 

id="aid73381_1" class="currentStepAnswer" value="No">No</label></div></li>

Javascript:


function calculateSum() {

    var e = document.getElementsByClassName('currentStepAnswer');

    var i; 

    var s = 0;

    for (i = 0; i < e.length; i++) {

       if (e[i].value == "Yes") { 

           s++ ; 

       }

 }

       $("#fallRiskTotal").html(s.toFixed(1));


}

-- 这个 JavaScript 在另一个显示 Q 和 A 的函数中被调用


   if (currentQuestionNum == 11) {

      riskAssessTitles += "<hr>";

      riskAssessTitles += "<h4 class='left' style='margin: 20px 0px 15px 0px';>Total:

      <span id='fallRiskTotal' style='padding:20px 0px 0px 3px';></span></h4>";

      $(riskAssessTitles).insertAfter($('#qid' + qid + '_' + instanceNum));

  }


  calculateSum();


互换的青春
浏览 82回答 2
2回答

倚天杖

// function to return # of 'Yes'sfunction calculateSum() {&nbsp; &nbsp; return $(".currentStepAnswer[value='Yes']:checked").length;};$(function() {&nbsp; &nbsp; $(".currentStepAnswer").change(function() {&nbsp; &nbsp; &nbsp; &nbsp; $("#fallRiskTotal").html(calculateSum().toFixed(1));&nbsp; &nbsp; });});

达令说

考虑以下示例。$(function() {&nbsp; function calculateSum() {&nbsp; &nbsp; return $(".currentStepAnswer[value='Yes']:checked").length;&nbsp; }&nbsp; function calculatePerc() {&nbsp; &nbsp; var y = $(".currentStepAnswer[value='Yes']:checked").length;&nbsp; &nbsp; var t = $("ul li").length;&nbsp; &nbsp; return y / t;&nbsp; }&nbsp; $(".currentStepAnswer").change(function() {&nbsp; &nbsp; $("#fallRiskTotal").html(calculateSum().toFixed(2) + " " + (calculatePerc() * 100).toFixed(2) + "%");&nbsp; });});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><ul>&nbsp; <li id="qid61915_1" value="11" style="margin: 20px 0px 30px 0px" required="true">Test Question&nbsp; &nbsp; <div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_1"&nbsp;id="aid73380_1" class="currentStepAnswer" value="Yes">Yes</label></div>&nbsp; &nbsp; <div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_1"&nbsp;id="aid73381_1" class="currentStepAnswer" value="No">No</label></div>&nbsp; </li>&nbsp; <li id="qid61915_2" value="12" style="margin: 20px 0px 30px 0px" required="true">Test Question&nbsp; &nbsp; <div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_2"&nbsp;id="aid73380_2" class="currentStepAnswer" value="Yes">Yes</label></div>&nbsp; &nbsp; <div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_2"&nbsp;id="aid73381_2" class="currentStepAnswer" value="No">No</label></div>&nbsp; </li>&nbsp; <li id="qid61915_3" value="13" style="margin: 20px 0px 30px 0px" required="true">Test Question&nbsp; &nbsp; <div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_3"&nbsp;id="aid73380_3" class="currentStepAnswer" value="Yes">Yes</label></div>&nbsp; &nbsp; <div class="radio answerRadio width-70"><label><input type="radio" name="qid61915Radio_3"&nbsp;id="aid73381_1" class="currentStepAnswer" value="No">No</label></div>&nbsp; </li></ul><div id="fallRiskTotal"></div>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答