javascript - 如何通过javascript更新html中的数字?

标签 javascript jquery html css

选民投票后,我想更新计数。

<form class="vote-form" action="">
  <div id="{{key}}" class="vote-count">{{votes}}</div>
  <input class="vote-button" type="submit" class="text-button" value="vote+"/>
  <input type="hidden" class="brag" name="brag" value="{{key}}">
  <input type="hidden" class="voter" name="voter" value="{{current_user.fb_id}}">
</form>

<script type="text/javascript">
  $(function() {  
    $('.error').hide();  
    $(".vote-form").submit(function() { 
      var inputs = $(this).find('input:hidden');
      var key = $('input.brag', this).val();
      $.ajax({
        type: "POST",  
        url: "/bean",  
        data: inputs,  
        success: function() {  
          //not sure how to do this, I want to increment {{votes}}
          $('#' + key).innerHTML = "foo"; 
        }  
      });  
      return false;
    });  
  });
</script>

最佳答案

var $div = $('#' + key),
    intValue = parseInt($div.html(), 10);

$div.html(++intValue);

关于javascript - 如何通过javascript更新html中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4630487/

相关文章:

javascript - SAPUI5中标准控件的重写方法

javascript - 如何在我的 HTML 网页中使用 JSON 文件

jQuery Fancybox 2.0.1 + 模态模式

javascript - 在 FF 中关注文本框不跟随警报?

jQuery 和 CSS : hide/show select options with a certain css class

javascript - 从 AngularJS 将文本插入 HTML

javascript - 如何创建导航侧菜单?

javascript - 纹理未正确映射到正方形 WebGL

javascript - 有没有办法将多个搜索功能组合成一个下拉选择?

javascript - 如何通过jquery获取对象值?