将最大长度设置为 jQuery 计算器的数字显示



我正在尝试制作一个jQuery ajax计算器,并且我已经为界面创建了一个html。

我的问题是我想设置显示器的最大长度。那只是 8 位数字。8 位数字后,它会显示"Err",但我应该能够在单击数字后清除"Err"。

这是我的代码

以下是修改后的代码(附有解释):

$(function(){
    var $display = $('#display');
    $display.val(0);
    var key = null;
    $(document).on('click', 'button.number', function() { 
        if ($display.val().length >= 8) { //If there are 8 or more characters
            $display.val("Err"); //Write Err
        } else if ($display.val() == "Err") { //If it says Err
            $display.val("0"); //Set it to zero
        } else { //If everything's fine
            $display.val(($display.val() == "0" ? "" : $display.val()) + $(this).val()); 
            //Add the pressed number to the display 
            //The ternary statement means "if it's zero, replace, else append"
        }
    });
});

最新更新