2012-02-17 62 views
0
$(function() { 
    $("#datepicker").datepicker({ 
     dateFormat: "dd-mm-yy", 
     minDate: -0 
    }); 
    $(".minus").click(function() { 
     var id = $(this).attr("id").substr(4); 
     var value = parseInt($("#value_" + id).val()); 
     if (value == 0) return false; 
     newval = parseInt(value) - 1; 
     $("#value_" + id).val(newval) 
    }); 
    $(".plus").click(function() { 
     var id = $(this).attr("id").substr(5); 
     var value = parseInt($("#value_" + id).val()); 
     newval = parseInt(value) + 1; 
     $("#value_" + id).val(newval) 
    }); 
    $(".value").keydown(function(event) { 
     // Allow only backspace and delete 
     if (event.keyCode == 46 || event.keyCode == 8) { 
      // let it happen 
     } else { 
      // Ensure that it is a number and stop the keypress 
      if (event.keyCode < 48 || event.keyCode > 57) { 
       event.preventDefault(); 
      } 
     } 
    }); 
});​ 

這是我當前的代碼但其沒有在這裏工作是以下我想:
我有20個輸入框-+按鍵都帶有不同勢計數器。
HTML例如:jQuery的提高輸入框的值

<tr> 
    <td><h4>Mozzarella met honing, tomaat en basilicum</h4></td> 
    <td width="100" valign="top">EUR 3,75</td> 
    <td width="150"> 
     <input type="hidden" name="naam_3" value="Kaas - Mozzarella met honing, tomaat en basilicum"> 
     <input type="hidden" name="prijs_3" value="3,75"> 
     <div class="minus" id="min_wit_3">-</div> 
     <input type="text" class="value" id="value_wit_3" name="aantal_3_wit" value="0"> 
     <div class="plus" id="plus_wit_3">+</div> 
     <div class="minus" id="min_bruin_3">-</div> 
     <input type="text" class="value" id="value_bruin_3" name="aantal_3_bruin" value="0"> 
     <div class="plus" id="plus_bruin_3">+</div> 
    </td> 
</tr> 

<tr> 
    <td>Boerenbrie</h4></td> 
    <td width="100" valign="top">EUR 4,00</td> 
    <td width="150"> 
     <input type="hidden" name="naam_4" value="Kaas - Boerenbrie"> 
     <input type="hidden" name="prijs_4" value="4"> 
     <div class="minus" id="min_wit_4">-</div> 
     <input type="text" class="value" id="value_wit_4" name="aantal_4_wit" value="0"> 
     <div class="plus" id="plus_wit_4">+</div> 
     <div class="minus" id="min_bruin_4">-</div> 
     <input type="text" class="value" id="value_bruin_4" name="aantal_4_bruin" value="0"> 
     <div class="plus" id="plus_bruin_4">+</div> 
    </td> 
</tr> 

每當我點擊+按鈕它必須是1,當我再次點擊它必須是2,依此類推。
當我點擊-按鈕時,它會反轉,但不能低於0.
希望任何人都知道解決方案。 < BR />
所以基本上我需要多輸入框

網站1個代碼:http://www.verruklik.nl/bestelformulier/ 嘗試,並按+按鈕沒有任何反應,而德劇本似乎是正確的。

+3

請只發布您的代碼中的相關章節。發佈無格式的代碼牆並不能讓人們很容易地幫助你。 – 2012-02-17 10:51:06

+1

@Scoobler如果我能代表你的話,我會的。 – 2012-02-17 10:54:19

+0

另外,嘗試更具體。你想做什麼,你做不到? 不要指望人們爲你做所有事情。 http://jsfiddle.net/也會很棒。 – 2012-02-17 10:54:30

回答

2

張貼在討論的代碼,工程 - 腳本這裏沒有工作的原因是由於datepicker

檢查代碼here刪除日期選擇器。
查看發佈的代碼here不能使用日期選擇器。
檢查使用日期選擇器的代碼here請注意添加的資源(在左側單擊管理資源)。

您的網站上,你已經包括jQuery UI的1.7.2兩個版本1.8.14 (線36和37)這可能會造成衝突,可以打破的JavaScript。

有網站上的另一個錯誤,也可以打破的東西:
Error: $("ul.sf-menu").supersubs is not a function
Source File: http://www.verruklik.nl/js/onLoad.js
Line: 1

一種在你的代碼中發現斷裂尖,使用瀏覽器控制檯,可能。
如果您使用的是谷歌Chrome,IE9按F12或Firefox瀏覽器與Web開發人員擴展按CTRL+SHIFT+J

1

你需要添加一個條件到你的$(".minus")點擊處理程序來檢查它是否會使值小於零。

$(".minus").click(function() { 
    var id = $(this).attr("id").substr(4); 
    var value = parseInt($("#value_"+id).val()); 

    if (value == 0) 
     return false; 

    newval = parseInt(value) - 1; 
    if (newval < 0) 
     newval = 0; 

    $("#value_"+id).val(newval) 
}); 
+0

這只是解決負數量,謝謝你。 – 2012-02-17 12:37:25