如何判断数字文本框的值是否为正数

How do you tell if the value of a number text box goes positive

本文关键字:是否 文本 何判断 判断 数字      更新时间:2023-09-26

可能是一个简单的答案,但由于某种原因,即使使用简单的Google搜索,我也找不到答案。

有数字文本框,我想知道数字的值是上升还是增加,我想做某事,如果值下降或减少,我希望它变成其他东西。

这是我现在拥有的简单 JS,但现在我只是在寻找文本框是否超过 1。 我想知道该值是否真的增加了。我正在使用jQuery。

我有一些简单的按钮将文本框的值增加 1 或增加 1。

如前所述,您将看到我需要一些帮助的部分。

====

=========

编辑:

添加了 JSFiddlehttp://jsfiddle.net/4v6wb2ys/2/

您将在左侧看到与会者总数。这将控制可以在右侧控制的数字。 我的问题部分是小提琴中"结果"中右侧面板底部的 CART 部分。购物车不会根据需要将商品添加到购物车。它添加一些,但在递减时添加更多项目,直到文本框的值达到 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);
....
....