加/减最大值输入

时间:2016-06-06 19:05:47

标签: jquery html button fiddle

我有一个加号/减号按钮,并希望用户不能选择超过20但不知道如何让它工作。我尝试使用min =“1”max =“5个属性,但它们不起作用。这是我的代码和指向小提琴的链接。https://jsfiddle.net/6n9298gp/

<form id='myform' method='POST' action='#' class="numbo">
<input type='button' value='-' class='qtyminus' field='quantity' style="font-weight: bold;" />
<input type='text' name='quantity' value='1' class='qty' style="margin-bottom: 0px !important" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
<input type='button' value='+' class='qtyplus' field='quantity' style="font-weight: bold;" />
</form>

<script type="text/javascript">
jQuery(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $('input[name='+fieldName+']').val(currentVal + 1);
            $('.qtyminus').val("-").removeAttr('style')
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(1);

        }
    });
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
    // Stop acting like a button
    e.preventDefault();
    // Get the field name
    fieldName = $(this).attr('field');
    // Get its current value
    var currentVal = parseInt($('input[name='+fieldName+']').val());
    // If it isn't undefined or its greater than 0
    if (!isNaN(currentVal) && currentVal > 1) {
        // Decrement one
        $('input[name='+fieldName+']').val(currentVal - 1);
    } else {
        // Otherwise put a 0 there
        $('input[name='+fieldName+']').val(1);
        $('.qtyminus').val("-").css('color','#aaa');
        $('.qtyminus').val("-").css('cursor','not-allowed');
    }
});
});
</script>

4 个答案:

答案 0 :(得分:4)

<input type="number" min="1" max="20" step="1">

然后您可以使用脚本来传递验证消息(仅因为数字字段的浏览器中内置的消息目前很差)。

这将删除对库的依赖性,遵循HTML规范,并且具有免费内置的可访问性。

如果仍需要+/-按钮来满足设计限制,请确保使用减号(&#8722;),然后在正确的字段类型之上逐步增强脚本。这样,任何jQuery不下载的场景(例如网络打嗝)或页面上出现脚本错误都不会导致整个事情崩溃。

如果您不满足+/-按钮的设计要求,请考虑完全丢失它们。

您也可以通过将模式匹配放入元素中,逐步增强对可能会遇到type="number"的浏览器(尽管您可以看到support is pretty good,而pattern support更好)而没有脚本的情况虽然这是一个非常优秀的案例:

<input type="number" min="1" max="20" step="1" pattern="[0-9]*">

您可以阅读more on type="number" in the HTML5 specthe language reference

答案 1 :(得分:1)

我在这里更新了jsfiddle:https://jsfiddle.net/6n9298gp/5/

基本上只是添加了一个块来检查当前值是否低于20以允许增量,否则显示你的&#34;不允许&#34;图标:

if (currentVal < 20)
{
      $('input[name='+fieldName+']').val(currentVal + 1);
      $('.qtyminus').val("-").removeAttr('style');
}
else
{
      $('.qtyplus').val("+").css('color','#aaa');
      $('.qtyplus').val("+").css('cursor','not-allowed');
}

在减量时还添加了一行以删除光标:

// Decrement one only if value is > 1
$('input[name='+fieldName+']').val(currentVal - 1);
$('.qtyplus').val("+").removeAttr('style');

答案 2 :(得分:0)

你做得对。

$('.qtyplus').click(function(e){
            // Stop acting like a button
            e.preventDefault();
            // Get the field name
            fieldName = $(this).attr('field');
            // Get its current value
            var currentVal = parseInt($('input[name='+fieldName+']').val());
            // If is not undefined
            if (!isNaN(currentVal) && currentVal < 20) {
                // Increment
                $('input[name='+fieldName+']').val(currentVal + 1);
            } else {
                // Otherwise put a 0 there
                //$('input[name='+fieldName+']').val(1);
                $(this).val("+").css('color','#aaa');
            $(this).val("+").css('cursor','not-allowed');

            }
        });

jQuery(document).ready(function(){
        // This button will increment the value
        $('.qtyplus').click(function(e){
            // Stop acting like a button
            e.preventDefault();
            // Get the field name
            fieldName = $(this).attr('field');
            // Get its current value
            var currentVal = parseInt($('input[name='+fieldName+']').val());
            // If is not undefined
            if (!isNaN(currentVal) && currentVal < 20) {
                // Increment
                $('input[name='+fieldName+']').val(currentVal + 1);
            } else {
                // Otherwise put a 0 there
                //$('input[name='+fieldName+']').val(1);
                $(this).val("+").css('color','#aaa');
            $(this).val("+").css('cursor','not-allowed');

            }
        });
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 1) {
            // Decrement one
            $('input[name='+fieldName+']').val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(1);
            $(this).val("-").css('color','#aaa');
            $(this).val("-").css('cursor','not-allowed');
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id='myform' method='POST' action='#' class="numbo">
    <input type='button' value='-' class='qtyminus' field='quantity' style="font-weight: bold;" />
    <input type='text' name='quantity' value='1' class='qty' style="margin-bottom: 0px !important" onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
    <input type='button' value='+' class='qtyplus' field='quantity' style="font-weight: bold;" />
    </form>

答案 3 :(得分:0)

尝试添加条件以检查小于最大值的值。

pthread