仅当用户得分达到某个数字时,按钮才会显示或启用?

时间:2019-05-16 12:46:17

标签: javascript html

我有一个单选按钮形式,其中每个选择都带有一个数值。分数在表格末尾汇总。用户需要评分一定数量才能提交申请。

我在html中禁用了提交按钮。我仅在用户得分超过60时才需要启用它。我尝试了一些代码变体,但按钮仍然处于禁用状态。谁能指出我正确的方向?

  </li>

  <p>Total Score:<span id="total">0</span></p>
     <input type="submit" name="submit_name" value="OK" 
   class="submit_class" id="SubmitButton" disabled/>
   </form>
   </body>

  <script 
 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
 </script>

  <script language="javascript">
  $(":radio").on("change", function() {
    var total = 0;
    $(":radio:checked").each(function() {
      total += Number(this.value);
    });

    $("#total").text(total);
  });

  //ENABLE SUBMIT BUTTON CODE ACCORDING TO SCORE
 function submitOnly() {
 var current = $('.total').filter(':checked');

 if (current.score >= 60) {
sbmtBtn.disabled = false;
 }
 };
 </script>

 </html> 

1 个答案:

答案 0 :(得分:1)

在您的jquery中,每次得分变化时,您都可以进行检查:

$(":radio").on("change", function() {
  var total = 0;
  $(":radio:checked").each(function() {
    total += Number(this.value);
    if (total > 60) {
      $("#SubmitButton").prop('disabled', false);
    }
  });

  $("#total").text(total);
});
<p>Total Score:<span id="total">0</span></p>
<input type="submit" name="submit_name" value="OK" class="submit_class" id="SubmitButton" disabled />

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>