我正在开发一个非常重要的项目,用户可以在其中更改"压力"通过复选框的两个元素。
这些复选框有两个不同的值,一个用于bar-one
,另一个用于bar-two
。所有这个系统都有一个"状态面板"如果一切正常,或者是否存在问题,那就说明了。
请注意,我不必使用回声或提醒消息,因为我需要根据两个小节的当前状态显示不同的div。
我在片段中做得最好,我是JavaScript的新手,所以请不要吝啬我的错误。
var
even = $('.even'),
high = $('.high'),
low = $('.low');
$('input').on('click', function() {
var emptyValue = 0;
$('input:checked').each(function() {
emptyValue += parseInt($(this).val());
});
$('.bar-one').css('width', emptyValue + '%').attr('aria-valuenow', emptyValue);
});
if (average === 5) {
even.show();
} else {
even.hide();
}
if (average >= 7) {
high.show();
} else {
high.hide();
}
if (average <= 3) {
low.show();
} else {
low.hide();
}
&#13;
.progress {
width: 100%;
height: 30px;
background-color: silver;
}
.bar-one {
background-color: blue;
}
.bar-two {
background-color: red;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="progress">
<div class="bar-one" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
<br>
<div class="progress">
<div class="bar-two" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
<br>
<div id="panel">
<input type="checkbox" value1="20" value2="5">
<input type="checkbox" value1="5" value2="20">
<input type="checkbox" value1="10" value2="10">
<input type="checkbox" value1="10" value2="-20">
<input type="checkbox" value1="-20" value2="10">
</div>
<div class="even">
Pressure is ok
</div>
<div class="high">
Pressure is high
</div>
<div class="low">
pressure is low
</div>
&#13;
非常感谢您的帮助!
答案 0 :(得分:2)
不确定您希望它如何发展。但这就是我要做的。也许看看我做了什么你可以弄清楚你想做什么。
var
even = $('.even'),
high = $('.high'),
low = $('.low');
var averageCount = $("#panel").find("input").length;
var average = 0; // not sure how you are calculating average so i just added this.
updateStatus(average); // just to start message boxes as default;
$('input').on('click', function() {
var emptyValue1 = 0;
var emptyValue2 = 0;
$('input:checked').each(function() {
emptyValue1 += parseInt($(this).attr("data-value1"));
emptyValue2 += parseInt($(this).attr("data-value2"));
});
average = (emptyValue1+emptyValue2)/averageCount;
$('.bar-one').css('width', emptyValue1 + '%').attr('aria-valuenow', emptyValue1);
$('.bar-two').css('width', emptyValue2 + '%').attr('aria-valuenow', emptyValue2);
updateStatus(average); // update the message box
});
function updateStatus(average){
if (average === 5) {
even.show();
} else {
even.hide();
}
if (average >= 7) {
high.show();
} else {
high.hide();
}
if (average <= 3) {
low.show();
} else {
low.hide();
}
}
.progress {
width: 100%;
height: 30px;
background-color: silver !important;
}
.bar-one {
background-color: blue !important;
}
.bar-two {
background-color: red !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="progress">
<div class="bar-one progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%">
</div>
</div>
<br>
<div class="progress">
<div class="bar-two progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%">
</div>
</div>
<br>
<div id="panel">
<input type="checkbox" data-value1="20" data-value2="5">
<input type="checkbox" data-value1="5" data-value2="20">
<input type="checkbox" data-value1="10" data-value2="10">
<input type="checkbox" data-value1="10" data-value2="-20">
<input type="checkbox" data-value1="-20" data-value2="10">
</div>
<div class="even">
Pressure is ok
</div>
<div class="high">
Pressure is high
</div>
<div class="low">
pressure is low
</div>
答案 1 :(得分:-1)
您没有初始化变量平均值。我把它放在三个,其余的代码似乎工作。
var
even = $('.even'),
high = $('.high'),
low = $('.low'),
average = 3;
$('input').on('click', function() {
var emptyValue = 0;
$('input:checked').each(function() {
emptyValue += parseInt($(this).val());
});
$('.bar-one').css('width', emptyValue + '%').attr('aria-valuenow', emptyValue);
});
if (average === 5) {
even.show();
} else {
even.hide();
}
if (average >= 7) {
high.show();
} else {
high.hide();
}
if (average <= 3) {
low.show();
} else {
low.hide();
}
.progress {
width: 100%;
height: 30px;
background-color: silver;
}
.bar-one {
background-color: blue;
}
.bar-two {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="progress">
<div class="bar-one" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
<br>
<div class="progress">
<div class="bar-two" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
<br>
<div id="panel">
<input type="checkbox" value1="20" value2="5">
<input type="checkbox" value1="5" value2="20">
<input type="checkbox" value1="10" value2="10">
<input type="checkbox" value1="10" value2="-20">
<input type="checkbox" value1="-20" value2="10">
</div>
<div class="even">
Pressure is ok
</div>
<div class="high">
Pressure is high
</div>
<div class="low">
pressure is low
</div>