【问题标题】:How to enable a disabled button jquery如何启用禁用的按钮jquery
【发布时间】:2016-02-13 13:01:29
【问题描述】:

我有这个问题。我的用户可以单击- 按钮从总数中减去 1。他们不能低于0。当我单击我的按钮到0 并单击+ 按钮转到0 上方时,它可以工作。但是,当我想再次单击 - 按钮时,它仍然处于禁用状态。

这是我的代码

$('#minderApple').click(function(){
    $("#appletotal").val( parseInt($("#appletotal").val()) - 1);
    if ($('#appletotal').val() < 1){
        $("#minderApple").prop("disabled",true);
    }else{
        $("#minderApple").prop("disabled",false);
    }
});

【问题讨论】:

    标签: jquery click disabled-input


    【解决方案1】:
    $('#minderApple').click(function(){
        if ((parseInt($("#appletotal").val()) - 1) < 1){
            $("#minderApple").attr("disabled","disabled");
        }else{
            $("#minderApple").removeAttr("disabled");
        }
    });
    

    【讨论】:

    • 你还在比较 string:int: if ($('#appletotal').val() &lt; 1)
    【解决方案2】:

    问题是因为val() 返回一个字符串,您将其与整数进行比较。您可以使用 parseInt() 将值转换为 int。此外,您可以通过将布尔值直接提供给prop() 来删除if 语句。试试这个:

    $('#minderApple').click(function() {
        var $appleTotal = $('#appletotal');
        var newVal = parseInt($appleTotal.val(), 10) - 1;
        $appleTotal.val(newVal);
        $(this).prop("disabled", newVal < 1);
    });
    

    Working example

    【讨论】:

    • 这不是一个有用的回应。你没有得到什么行为?你得到什么行为?控制台中的任何错误?查看您的 HTML 也会有很大帮助
    • 如果你点击 - 按钮就可以了。但是当您在输入字段中输入 5 并单击 - 按钮时,它不再起作用了
    • 这些细节应该已经放在问题中了。为此,您还需要检查inputchange 事件的值:jsfiddle.net/RoryMcCrossan/kzagbe83/2
    • 你是对的@RoryMcCrossan,需要提供完整的细节和研究。 +1
    【解决方案3】:
    <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
            <style>
    
                .active {
                    color:red;
                }
            </style>
        <script>
    
            $(document).ready(function () {
    
                $('#minderApple').click(function () {
                    $("#appletotal").val(parseInt($("#appletotal").val()) - 1);
                    if ($('#appletotal').val() < 1) {
                        $("#minderApple").prop("disabled", true);
                    } else {
                        $("#minderApple").prop("disabled", false);
                    }
                });
    
    
                $('#plus').click(function () {
                    $("#appletotal").val(parseInt($("#appletotal").val()) + 1);
                    if ($('#appletotal').val() >1) {
                        $("#minderApple").prop("disabled", false);
                    } else {
                        $("#minderApple").prop("disabled", false);
                    }
                });
    
            });
        </script>
        </head>
    
        <body>
            <input type="text" id="appletotal" value="5"/>
          <input type="button" id="minderApple"  value="-"/>
          <input type="button" id="plus"  value="+"/>
    
        </body>
    
        </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2014-12-02
      • 2010-12-08
      相关资源
      最近更新 更多