2014-12-22 77 views
-1

可能是一個簡單的答案,但由於某種原因,即使使用簡單的Google搜索,我也找不到答案。如何判斷數字文本框的值是否爲正

我有數字文本框,我想知道如果數值的價值增加或增量,我想做的事情,如果價值下降或減少,我希望它的東西。

這是我現在擁有的簡單的JS,但現在我只是在尋找文本框是否超過1.我真的想知道值是否實際增加了。 我正在使用jQuery。

我有一些簡單的按鈕按1或drecrementing增加文本框的值由1

您將在下面看到,我需要一些幫助,如提到的部分。

=============

編輯:

增加了的jsfiddle http://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(); 
      } 
    }); 
    }); 
+2

什麼問題與你有什麼代碼? 'foo> 0'顯示你的變量是否爲正值。 'foo <0',否定。 – philtune

+1

你可以做一個小提琴嗎? – Razorphyn

+0

當你明確這樣做時,你想*知道該值是否已經增加或減少了? - 'newMemVal = parseFloat(oldMemValue)+ 1' ...'newVal = parseFloat(oldValue) - 1;':/ – PeterKA

回答

1

這裏是序列:

.... 
.... 
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); 
.... 
.... 
+0

我會檢查一下。感謝指針。說得通。 – ClosDesign

相關問題