jQuery increment value of tag

后端 未结 7 766
小鲜肉
小鲜肉 2020-12-31 04:12

I have a number in a span tag. I need to get the value of the number in the span and then increment it inside jQuery. I know how to do this for a text input - can this be

相关标签:
7条回答
  • 2020-12-31 04:23
    $(".changeNumber").html(parseInt($(".changeNumber").html())+1)
    
    0 讨论(0)
  • 2020-12-31 04:24

    I assume you mean something like this?

    <span id="Nbr">123</span>
    

    If so, the InnerHTML property will give you the content of the span

    0 讨论(0)
  • 2020-12-31 04:26

    You can do something like:

    var value = parseInt($(".changeNumber").text(), 10) + 1;
    $(".changeNumber").text(value);
    

    JsFiddle Example:

    http://jsfiddle.net/stapiagutierrez/WXAvS/1/

    References:

    1. parseInt()
    2. text()
    0 讨论(0)
  • 2020-12-31 04:29

    This is the most complete answer.

    1. This solution avoids selector repetition
    2. Handles the increment gracefully if the span does not have a value yet.
    <span class="changeNumber"></span>
    
    var $number = $('.changeNumber');
    $number.html((parseInt($number.html(),10) || 0) + 1);
    
    0 讨论(0)
  • 2020-12-31 04:35

    Something like this:

    var $span = $('#mySpanId');
    $span.text(Number($span.text()) + 1);
    

    http://jsfiddle.net/mattball/Cf834/

    0 讨论(0)
  • 2020-12-31 04:43
    var spanVal=$("span").html();
    if(!isNaN(spanVal)){
    
    alert("value incremented"+ (++spanVal));
    }
    

    http://jsfiddle.net/c84F4/

    0 讨论(0)
提交回复
热议问题