2013-03-29 40 views
0

我們正在嘗試在ASP.Net文本框上使用Javascript增量和減法來調高數值。 默認文本框設置爲0.ASP.Net上的Javascript/jQuery增量文本框

我們追加了2個div,它們使用Javascript來增加和減少ASP.Net文本框中的值。值得到改變,但...

我們也有問題,其中的Javascript被認爲應該防止值低於0.然而,當我們遞減到0以下時,文本框變爲空白,然後當我們嘗試增加,我們得到一個NaN值。我的計劃是當值變爲'0'時使遞減按鈕消失。 任何幫助表示讚賞。是否可以在ASP.Net文本框上使用Javascript/jQuery增量器/遞減器?

任何有關如何在值命中'0'時使減數按鈕消失並在值大於'0'時重新出現的指針。 PLUS如果可以在ASP.Net文本框上使用Javascript遞增/遞減。

在此先感謝。

這裏是低於我的javascript:

if (Modernizr.mq("screen and (max-width:767px)")) { 
     if ($('.product-variant-list td span.quantity input.numerictextboxtext:not([disabled])')) { 
      $('input.numerictextboxtext:not([disabled])').parent().append('<div class="incButton button">+</div><div class="decButton button">-</div>'); 
     } 

     if ($('input.numerictextboxtext').val() == 0) { 
      $(".decButton").hide(); 
     } 
     if ($('input.numerictextboxtext').val() >= 1) { 
      $(".decButton").show(); 
     } 

     $(".button").click(function() { 
      var $button = $(this); 
      var oldValue = $button.parent().find("input.numerictextboxtext").val(); 

      //TODO: See if you can show and hide button on based on value of textbox 
      if(oldValue == 0){ 
       $(".decButton").hide(); 
      } 
      if (oldValue >= 1) { 
       $(".decButton").show(); 
      } 

      if ($button.text() == "+") { 
       var newVal = parseFloat(oldValue) + 1; 

      } else { 
       // Don't allow decrementing below zero - supposedly. But this does not work in our case 
       if (oldValue >= 1) { 
        var newVal = parseFloat(oldValue) - 1; 
       } 
      } 
      $button.parent().find("input.numerictextboxtext").val(newVal); 
     });//End button click 
    }//End Modernizr check 

回答

0

回答我們的問題是在這裏。我希望這可以幫助別人。

 if (Modernizr.mq("screen and (max-width:767px)")) { 
     if ($('.product-variant-list td span.quantity input.numerictextboxtext:not([disabled])')) { 
      $('input.numerictextboxtext:not([disabled])').parent().append('<div class="incButton button">+</div><div class="decButton button">-</div>'); 
     } 
     if ($('input.numerictextboxtext').val() == 0) { 
      $(".decButton").hide(); 
     } 
     $(".button").click(function() { 
      var $button = $(this); 
      var oldValue = $button.parent().find("input.numerictextboxtext").val(); 
      var newVal = oldValue; 
      //Hide .decButton for oldValue 
      if (newVal == 0 || oldValue == 0) {$button.parent().find(".decButton").hide(); oldValue = 0} 
      else {$button.parent().find(".decButton").show();} 
      if ($button.text() == "+") { var newVal = parseFloat(oldValue) + 1;} else { 
       // Don't allow decrementing below zero 
       if (oldValue >= 1) {var newVal = parseFloat(oldValue) - 1;} 
      } 
      //Hide .decButton for newVal 
      if (newVal == 0) {$button.parent().find(".decButton").hide();} 
      else {$button.parent().find(".decButton").show();} 
      $button.parent().find("input.numerictextboxtext").val(newVal); 
     });//End button click 
    }//End Modernizr check 
1

我想你的問題之一可能是在這裏。我已經改變了格式,讓問題更清楚。

if ($button.text() == "+") { 
    var newVal = parseFloat(oldValue) + 1; 
} else { 
    // Don't allow decrementing below zero - supposedly. But this does not work in our case 
    if (oldValue >= 1) { 
    var newVal = parseFloat(oldValue) - 1; 
    } 
} 
$button.parent().find("input.numerictextboxtext").val(newVal); 

newValif子句和else條款中聲明。但是,然後你將它用在這些子句之外的表達式中。所以當你使用newVal時,val()函數沒有定義。

嘗試聲明它在哪裏你定義oldVal

$(".button").click(function() { 
     var $button = $(this); 
     var oldValue = $button.parent().find("input.numerictextboxtext").val(); 
     var newValue = oldValue; // Declaring the variable here gives it 
           // the same scope as oldValue. Giving it 
           // oldValue as a default value means that it 
           // will have a value even if not assigned to again. 

     //TODO: See if you can show and hide button on based on value of textbox 
     if(oldValue == 0){ 
      $(".decButton").hide(); 
     } 
     if (oldValue >= 1) { 
      $(".decButton").show(); 
     } 

     if ($button.text() == "+") { 
      newVal = parseFloat(oldValue) + 1; 
     } else { 
      // Don't allow decrementing below zero 
      if (oldValue >= 1) { 
       newVal = parseFloat(oldValue) - 1; 
      } 
     } 
     $button.parent().find("input.numerictextboxtext").val(newVal); 
    });//End button click 
+0

你能舉一個這樣的例子嗎?我不清楚你想說什麼。如果我將它從if/else中取出,而不是以舊值> = 1爲目標值。 – ClosDesign

+0

當然。我會編輯我的答案。 –

+0

其實,我看到另外一個問題。讓我再次編輯它。 –