简体   繁体   English

第二次单击按钮不起作用(JQuery)

[英]Second click on button is not working (JQuery)

I have a onClick function work on button click event.我有一个 onClick 函数处理按钮单击事件。
The problem is this function is working for one time only for each button问题是此功能仅对每个按钮工作一次

HTML: HTML:

<div id="playerScore">120</div>
            <div class="col-md-12 bg-dark">
               <div class="btn-group d-flex align-content-stretch" role="group" aria-label="Basic example">
                  <button type="button" data-score="5" class="btn scoreBtn btn-secondary">5</button>
                  <button type="button" data-score="6" class="btn scoreBtn btn-secondary">6</button>
                  <button type="button" data-score="7" class="btn scoreBtn btn-secondary">7</button>
                  <button type="button" data-score="8" id="scoreBtn" class="btn scoreBtn btn-secondary">8</button>
               </div>
            </div>
            <div class="col-md-12 bg-dark">
               <div class="btn-group d-flex align-content-stretch" role="group" aria-label="Basic example">
                  <button type="button" data-score="9" class="btn scoreBtn btn-secondary">9</button>
                  <button type="button" data-score="10" class="btn scoreBtn btn-secondary">10</button>
                  <button type="button" data-score="11" class="btn scoreBtn btn-secondary">11</button>
                  <button type="button" data-score="12" class="btn scoreBtn btn-secondary">12</button>
               </div>
            </div>

JQuery:查询:

 var playerScore = 100; //starting score
  $('#playerScore').html(playerScore); // set starting player score

$(".scoreBtn").click(function() {
         var getCurrentScore = playerScore; //get player score
         var clickedScore = $(this).attr("data-score"); // get clicked button score
         var newScore = getCurrentScore - clickedScore; // subtract current score from clicked button score

         $('#playerScore').html(newScore); //update current score

      });

When I click any button for the first it worked but when i click the same button second time it does not work.当我第一次单击任何按钮时它起作用,但是当我第二次单击同一个按钮时它不起作用。

The player score is defined at 100, and you're not getting the updated score on button click.玩家得分定义为 100,并且您不会在单击按钮时获得更新的得分。 You can fix this by updating the score in your function, which can be done by doing:您可以通过更新函数中的分数来解决此问题,这可以通过执行以下操作来完成:

var getCurrentScore = $('#playerScore').text(); //get player score

Your function becomes:你的功能变成:

$(".scoreBtn").click(function() {
     var getCurrentScore = $('#playerScore').text(); //get player score
     var clickedScore = $(this).attr("data-score"); // get clicked button score
     var newScore = getCurrentScore - clickedScore; // subtract current score from clicked button score

     $('#playerScore').html(newScore); //update current score
});

Try it out in the following snippet:在以下代码段中尝试一下:

 var playerScore = 100; //starting score $('#playerScore').html(playerScore); // set starting player score $(".scoreBtn").click(function() { var getCurrentScore = $('#playerScore').text(); //get player score var clickedScore = $(this).attr("data-score"); // get clicked button score var newScore = getCurrentScore - clickedScore; // subtract current score from clicked button score $('#playerScore').html(newScore); //update current score });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="playerScore">120</div> <div class="col-md-12 bg-dark"> <div class="btn-group d-flex align-content-stretch" role="group" aria-label="Basic example"> <button type="button" data-score="5" class="btn scoreBtn btn-secondary">5</button> <button type="button" data-score="6" class="btn scoreBtn btn-secondary">6</button> <button type="button" data-score="7" class="btn scoreBtn btn-secondary">7</button> <button type="button" data-score="8" id="scoreBtn" class="btn scoreBtn btn-secondary">8</button> </div> </div> <div class="col-md-12 bg-dark"> <div class="btn-group d-flex align-content-stretch" role="group" aria-label="Basic example"> <button type="button" data-score="9" class="btn scoreBtn btn-secondary">9</button> <button type="button" data-score="10" class="btn scoreBtn btn-secondary">10</button> <button type="button" data-score="11" class="btn scoreBtn btn-secondary">11</button> <button type="button" data-score="12" class="btn scoreBtn btn-secondary">12</button> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM