Javascript 如何判断数字文本框的值是否为正数
可能是一个简单的答案,但由于某种原因,我无法找到答案,即使是一个简单的谷歌搜索 我有一个数字文本框,我想知道数字的值是上升还是递增,我想做些什么,如果值下降或递减,我想做些别的 这是我现在拥有的简单JS,但现在我只是在寻找文本框是否超过1。我想知道这个值是否真的增加了。 我正在使用jQuery 我有一些简单的按钮,将文本框的值增加1或将数据增加1 您将看到下面的部分,其中我需要一些帮助 =============Javascript 如何判断数字文本框的值是否为正数,javascript,jquery,Javascript,Jquery,可能是一个简单的答案,但由于某种原因,我无法找到答案,即使是一个简单的谷歌搜索 我有一个数字文本框,我想知道数字的值是上升还是递增,我想做些什么,如果值下降或递减,我想做些别的 这是我现在拥有的简单JS,但现在我只是在寻找文本框是否超过1。我想知道这个值是否真的增加了。 我正在使用jQuery 我有一些简单的按钮,将文本框的值增加1或将数据增加1 您将看到下面的部分,其中我需要一些帮助 ============= $(".AddMinusButton").on('click touchs
$(".AddMinusButton").on('click touchstart', function (event) {
var $button = $(this);
var oldValue = $button.parent().find("input.attendeeQuantityInput").val();
var newVal = oldValue;
//Increment and decrement the value of the textbox
if ($button.text() == "+") {
newVal = parseFloat(oldValue) + 1;
} else {
// Don't allow decrementing below zero
if (oldValue >= 1) {
newVal = parseFloat(oldValue) - 1;
}
}
$InputItem = $('.ProductDetailPage').find("input[data-attendee='" + gaData + "']");
$InputItem.each(function() {
//I have some values for vars $list and li but those are not important for this.
//THIS IS WHERE I NEED THE ANSWER TO MY QUESTION
if ($(this).val() >= 1) { //This line should probably be were the 'IF value increments'
$list.append(li);
} else {
$list.find("li." + attendee + parentSection).remove();
}
});
});
编辑:
添加了一个JSFiddle
您将看到左侧共有与会者。这将控制可在右侧控制的数字。我的问题部分是小提琴中“结果”右面板底部的购物车部分。
购物车不会根据需要向购物车添加项目。它会添加一些,但在递减时会添加更多项,直到文本框的值达到0。然后它会移除它们。
这是我要问的问题的主要部分。我需要添加一个外卖项目,因为右面板中的值是递增和递减的。
我知道我的JS很难看。如果有改进,请告诉我如何修复
=======================
$(".AddMinusButton").on('click touchstart', function (event) {
var $button = $(this);
var oldValue = $button.parent().find("input.attendeeQuantityInput").val();
var newVal = oldValue;
//Increment and decrement the value of the textbox
if ($button.text() == "+") {
newVal = parseFloat(oldValue) + 1;
} else {
// Don't allow decrementing below zero
if (oldValue >= 1) {
newVal = parseFloat(oldValue) - 1;
}
}
$InputItem = $('.ProductDetailPage').find("input[data-attendee='" + gaData + "']");
$InputItem.each(function() {
//I have some values for vars $list and li but those are not important for this.
//THIS IS WHERE I NEED THE ANSWER TO MY QUESTION
if ($(this).val() >= 1) { //This line should probably be were the 'IF value increments'
$list.append(li);
} else {
$list.find("li." + attendee + parentSection).remove();
}
});
});
顺序如下:
....
....
var newVal = oldValue;
var incremented; //declare a new variable;
....
....
newMemVal = parseFloat(oldMemValue) + 1;
incremented = true; //after you increment the value set the variable to true
.....
.....
newVal = parseFloat(oldValue) - 1;
incremented = false; //after you increment the value set the variable to false
....
....
if ( incremented ) { //Now you know what you did!! 'incremented' remembers for you.
$list.append(li);
....
....
你的代码有什么问题
foo>0
显示变量是否为正foo<0
,负数。你会拉小提琴吗?你想知道当你明确地这样做时,这个值是增加了还是减少了?--newMemVal=parseFloat(oldMemValue)+1
newVal=parseFloat(oldValue)-1
:/@philtune如果这个数字本身是正数,我想知道这个值是增加还是减少,如果这个值是正数,我想知道。看起来你忽略了明显的问题。重读彼得卡的评论。