javascript中数字的一些常规操作
1,禁止输入 - (减号、负号)
// html <input type="number" class="no-negative"> // js $(".no-negative").on("keydown", function (e) { if (e.keyCode == 109 || e.keyCode == 229 || e.keyCode == 189) { return false; } }) $(".no-negative").on("prototypechange input", function () { var val = $(this).val().substring(0, $(this).val().length - 1); if ($(this).val().indexOf('-') != -1 || !$(this).val()) { $(this).val(val); } })
2、禁止输入小数点
// html <input type="number" class="no-decimal"> // js $(".no-decimal").on("keydown", function (e) { if (e.keyCode == 110 || e.keyCode == 229 || e.keyCode == 190) { return false; } })