[英]If number is less than zero and a negative number hide it jquery
Having the toughest time writing in the function to hide numbers that are equal to or less than zero. 在函数中编写最困难的时间来隐藏等于或小于零的数字。 Here is the function I want to write it in at: 这是我想在其中编写的函数:
function isItANumber() {
var increased = parseInt($("#increasedRevenueValue"));
if(isNaN(increased)) {
$("#increasedRevenueValue").hide();
}
}
Any thoughts? 有什么想法吗? Should I use OR
? 我应该使用OR
吗?
EDIT: Here is the fiddle of my code http://jsfiddle.net/YVcj7/ 编辑:这是我的代码的小提琴http://jsfiddle.net/YVcj7/
Try the following, Im taking a guess at what element your using. 请尝试以下方法,我猜测您使用的是什么元素。
function isItANumber() {
var increased = parseInt($("#increasedRevenueValue").text());
if(isNaN(increased) || increased <= 0) {
$("#increasedRevenueValue").hide();
}
}
isItANumber();
And pure js.. because do we really need jQuery for this? 和纯js ..因为我们真的需要 jQuery吗?
function isItANumber(el) {
var increased = parseInt(el.innerText);
if(isNaN(increased) || increased <= 0) {
el.style.display = 'none';
}
}
var element = document.getElementById("increasedRevenueValue");
isItANumber(element);
I think you want 我想你要
if(isNaN(increased) || increased <= 0){
$("#increasedRevenueValue").hide();
}
I don't think you can parseInt a jQuery object. 我认为您不能parseInt一个jQuery对象。 I would try parseInt($("#increasedRevenueValue").text());
我会尝试parseInt($("#increasedRevenueValue").text());
or parseInt($("#increasedRevenueValue").val());
或parseInt($("#increasedRevenueValue").val());
depending on what #increasedRevenueValue
is. 取决于#increasedRevenueValue
是什么。
It looks like your function is doing a bit of double duty (getting the value of a field, checking if it is a number, etc.). 似乎您的函数正在执行一些双重任务(获取字段的值,检查它是否为数字,等等)。
First, I would recommend checking that the value is numeric. 首先,我建议检查该值是否为数字。 This question has a very good isNumeric function
that you can leverage. 这个问题有一个很好的isNumeric function
,您可以利用。
In that case, you just need to do the following: 在这种情况下,您只需要执行以下操作:
function isItANumber(value) {
var value = $("#increasedRevenueValue").val() || $("#increasedRevenueValue").text() //get the value if this is an input field, or fallback on the text otherwise.
var isNumber = isNumeric(value);
var isNegative = value < 0;
if (isNumber && isNegative) {
$("#increasedRevenueValue").hide();
}
}
As people have mentioned, if the element is a input field, your 'increased' should be: 正如人们所提到的,如果元素是输入字段,则“增加”应为:
var increased = parseInt($("#increasedRevenueValue").val(), 10);
If its a div/span/paragraph or something along those lines it should be. 如果它是一个div / span / paragraph或类似的东西,应该是。
var increased = parseInt($("#increasedRevenueValue").text(), 10);
Then simply do an if loop. 然后只需执行一个if循环。
if(increased =< 0) {
$("#increasedRevenueValue").hide();
}
Depending on how your use of this function is you might need to consider if $("#increasedRevenueValue").val() is a empty value, paresint will make it into 0. 根据使用此函数的方式,您可能需要考虑$(“#increasedRevenueValue”)。val()是否为空值,paresint会将其设为0。
I am going to assume that $("#increasedRevenueValue")
is an input element, 我假设$("#increasedRevenueValue")
是一个输入元素,
function isItANumber() {
//change it to $("#increasedRevenueValue").text() if it is div/span
var increased = parseInt($("#increasedRevenueValue").val(), 10);
if(isNaN(increased) || increased <= 0) {
$("#increasedRevenueValue").hide();
}
}
我会尝试: http : //jsfiddle.net/pratik136/6J7xz/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.