Javascript JQuery-如果值是0,则隐藏按钮,否则,则显示 [英] Javascript JQuery - Hide a button if value is 0, display if not
问题描述
我有这个:
<input type="button" id="total_items" value="0">
该值随着项目添加到站点而增加. 我需要的是将其隐藏,如果该值为0,则随着该值的增加而开始显示.
The value is increasing as items added to the site. What I need is to hide it if the value is 0 and start to displaying as the value is increasing.
我知道JQuery可以添加一个CSS display:none选项,但是我不知道如何.谢谢您的帮助!
I know JQuery has an option to add a css display:none option, but I don't know how. Thank you for all the help!
推荐答案
如果ID为total_items
的元素是固定的,则可以使用css3
的属性选择器将visibility
设置为hidden
,然后然后调用增量逻辑以使其再次可见.
if the element with id total_items
is fixed then you can use the css3
's attribute selector to make the visibility
as hidden
and then call the increment logic to make it visible again.
这是一个基于total_items
的可见性的示例代码片段
Here is a sample snippet handling the visibility of the total_items
based on the value it has:
var btntotal_item = document.getElementById('total_items');
function incrementValue() {
btntotal_item.value++;
}
function decrementValue() {
btntotal_item.value--;
if (btntotal_item.value < 0)
btntotal_item.value = 0;
}
#total_items[value="0"] {
visibility: hidden
}
<input type="button" id="total_items" value="0">
<input type="button" onclick="incrementValue()" value="increment by 1"><input type="button" onclick="decrementValue()" value="decrement by 1">
这篇关于Javascript JQuery-如果值是0,则隐藏按钮,否则,则显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!