【问题标题】:how to change the progressive bar class from progress-bar-success to progress-bar-danger when the percentage is less than or equal to 50%当百分比小于或等于 50% 时,如何将进度条类从 progress-bar-success 更改为 progress-bar-danger
【发布时间】:2019-12-04 18:49:12
【问题描述】:

当工作正常时我有一个进度条,但我想要实现的是如果百分比低于 50%,我希望进度条从绿色变为红色。我在 jQuery 中工作,但没有得到结果,请问我可以得到一些帮助吗?

提前致谢

<td>
    <span class="header-text">
        <div class="progress-bar progress-bar-success progress-bar-striped active progressbartextcolor" id="below50percent" role="progressbar" aria-valuenow="25" aria-valuemin="0" v-bind:style="{width: customerLoan.progress + '%'}" aria-valuemax="100">{{customerLoan.progress || "0"}}%</div>
    </span>
</td>

<script>
    $(document).ready(function(){
        var bars = 
        $('{{customerLoan.progress}}');
        for (i = 0; i < bars.length; i++) 
        {
            console.log(i);
            var progress = $(bars[i]).attr('{{customerLoan.progress}}');                             
            $(bars[i]).width(progress + '%');
            if (progress <= "50") 
            {                                            
                $(bars[i]).addClass("progress-bar-danger");
            }
        }
    });
</script>

【问题讨论】:

  • 我很确定 $('{{customerLoan.progress}}'); 不会做你认为它做的事情,以及其他问题。我猜你使用某种插件?你使用把手的事实让我觉得你也在使用某种模板引擎?也许是角?我们需要一个minimal reproducible example。我的猜测是,您无法使用 jquery 与之交互
  • 无法理解 {{customerLoan.progress}} 是什么,这里的想法很简单,根据您只需要从元素中添加/删除所需类的进度
  • {{customerLoan.progress}} 指定宽度。
  • 那么你为什么要尝试使用宽度作为 jquery 选择器?你期望它匹配什么?我建议您阅读 jquery 选择器的工作原理,因为这根本不起作用api.jquery.com/category/selectors

标签: javascript jquery vue.js


【解决方案1】:

欢迎来到 SO。我建议您不要将 jQuery 用于此类操作。 Vue.js 有内置指令,称为v-bind:class。您所要做的就是创建具有所有类要求的计算对象,然后将其绑定到您的进度条。这里我有一个可行的解决方案:

new Vue({
  el: "#app",
  data: {
    customerLoan: {
      progress: 40
    }
  },
  methods: {},
  computed: {
    classObject: function() {
      return {
        'bg-success': this.customerLoan.progress >= 50,
        'bg-danger': this.customerLoan.progress < 50
      }
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<table id="app">
  <tr>
    <td>
      <span class="header-text">
				<div class="
				progress-bar  
				progress-bar-striped 
				active 
				progressbartextcolor" 
				id="below50percent" 
				role="progressbar" 
				aria-valuenow="25" 
				aria-valuemin="0" 
				aria-valuemax="100"
				v-bind:class='classObject'>
					{{ customerLoan.progress }}%
				</div>
			</span>
    </td>
  </tr>
</table>

您可以手动更改 customerLoan.progress 以确保类更改。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多