2012-02-17 91 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