我在页面上使用css qtyButton有多个按钮:
我想将css添加到值与给定值匹配的唯一按钮。我使用了以下代码,但是当我尝试选择按钮值时,它给我警报框中未定义的错误。

 <button class="qtyButton">1</button>
 <button class="qtyButton">2</button>
 <button class="qtyButton">3</button>





$(document).on('click','.arrow',function(e){

            var curr_qty=$('#qty').val();
            var text = $('.qtyButton').filter(function () {
                  return this.value== curr_qty}).css('color', 'blue');
            });

最佳答案

$(document).on('click', function(e) {
  var curr_qty = 2
  $('.qtyButton').each(function() {
    if (parseInt($(this).text()) === curr_qty) $(this).css('color', 'blue')
  })
})

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="qtyButton">1</button>
<button class="qtyButton">2</button>
<button class="qtyButton">3</button>

08-18 03:38