【问题标题】:Progress bar update width when page load?页面加载时进度条更新宽度?
【发布时间】:2015-08-14 07:04:58
【问题描述】:

当我加载页面时,我想在表格字段中显示进度条,如下所示:

<% @beacons.each do |beacon| %>
...
    <td><div id="progressbar" class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="10"
      aria-valuemin="0" aria-valuemax="100" style="width:100%" ><%= beacon.power %>%</div></td>

但是我不能beacon.power更新进度条的宽度,我不需要实时刷新,我只想在加载这个页面时,进度条的宽度可以使用我的数据库数据:beacon.power显示

我的网站是

IP/信标

我的控制器:

  def index
    @beacons = Beacon.all

    respond_to do |format|
      format.html { @beacons }
      format.json { @beacons }
    end
  end

我尝试使用javascript:

$(document).ready(function() {
    $.get("beacons.json", function(data){
      $("#progressbar").css('width', data.power+'%')
    }
    });
});

它不起作用,我的设置有什么问题?

【问题讨论】:

  • 你怎么知道它不起作用?如果有错误消息,请将其全文添加到您的问题中。

标签: javascript jquery css ruby-on-rails


【解决方案1】:

我不相信 jQuery 会自动解析 $.get 方法返回的 JSON。我认为您可能需要手动执行此操作。像这样……

$(document).ready(function() {
    $.get("beacons.json", function(data){
      parsedData = $.parseJSON(data);
      $("#progressbar").css('width', parsedData.power + '%')
    }
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多