我在按钮单击事件上有一个onClick函数。
问题在于此功能仅对每个按钮起作用一次
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
});
当我第一次单击任何按钮时,它都起作用,但是当我第二次单击同一按钮时,则不起作用。
答案 0 :(得分:0)
玩家得分被定义为100,而单击按钮并没有得到更新的得分。您可以通过更新函数中的分数来解决此问题,方法是:
var getCurrentScore = $('#playerScore').text(); //get 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
});
在以下代码段中尝试一下:
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>