簡體   English   中英

jQuery中的按鍵事件

[英]Key Press Event In Jquery

我有許多形式的輸入類型。現在我希望用戶只能在輸入類型中輸入整數值。輸入可以像這樣110.00,之后只有兩個值 。但是我無法獲得此功能。

我已經完成了整數輸入,但是我沒有得到如何做到這一點: 代碼

$(".amount_class").live("keypress",function(e){
        var charCode = (e.which) ? e.which : e.keyCode;
        var Enteted = String.fromCharCode(e.which).toLowerCase();
        if ((charCode >= 48 && charCode <= 57) || charCode == 8 || charCode == 9 || charCode == 37 || charCode == 39 || (charCode == 46 && Enteted != '.'))
            return true;
        else
        return false;
    });

這些值是數量,可以是十進制,但十進制符號后不能超過兩個。請幫助我

我已經做到了,並為我工作得很好:

$('.salary').live("keypress",function(e) {
        var charCode = (e.which) ? e.which : e.keyCode;
        var Enteted = String.fromCharCode(e.which).toLowerCase();
        if(!((charCode >= 48 && charCode <= 57) || charCode == 8 || charCode == 9 || (charCode == 37 && Enteted !='%') || charCode == 39 || charCode == 46)) {
            e.preventDefault();
        } 

        if(charCode == 46 && $(this).val().indexOf('.') != -1 && Enteted ==".") {
            e.preventDefault();
        } // prevent if already dot
        if(charCode == 46  && Enteted =="." && !$(this).val()) {
            e.preventDefault();
        } 
        if($(this).val().indexOf('.')!=-1){
            if($(this).val().split(".")[1].length >= 2){ 
                 if(charCode != 8  && charCode != 46) e.preventDefault();
                if( isNaN( parseFloat( this.value ) ) ) return;
                this.value = parseFloat(this.value).toFixed(2);
            }  
         }            
         return this;
    });

用途如下

HTML

<input type="text" id="checkDecimal" class="decimal" />

JS

$(function () {
     $('#checkDecimal').bind('paste', function () {
        var self = this;
        setTimeout(function () {
            if (!/^\d*(\.\d{1,2})+$/.test($(self).val())) $(self).val('');
        }, 0);
    });

    $('#checkDecimal').keypress(function (e) {
        var character = String.fromCharCode(e.keyCode)
        var newValue = this.value + character;
        if (isNaN(newValue) || parseFloat(newValue) * 100 % 1 > 0) {
            e.preventDefault();
            return false;
        }
    });
});

在這里看看。

正則表達式 -/ /^\\d+(\\.\\d{0,2})?$/g;

我在這里做了實驗

<!DOCTYPE html>
<html>
<head>
<script
src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$('#input_field').keyup(function(e) {
    var regex = /^\d+(\.\d{0,2})?$/g;
    if (!regex.test(this.value)) {
        this.value = '';

    }
});
  });
</script>
</head>
<body>
<input type= "text" id = "input_field" name ="input_field" value=""/>
</body>
</html>

邏輯是每次用戶輸入數字時,您都必須檢查兩件事。

  1. 用戶是否輸入了小數點?
  2. 小數點后兩位是否多?

對於第一個,您可以使用$(this).val().indexOf('.') != -1

對於第二個,可以使用$(this).val().substring($(this).val().indexOf('.'), $(this).val().indexOf('.').length).length > 2

這是代碼:

$('.amount_class').keypress(function (event) {
    if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
        event.preventDefault();
    }

    var text = $(this).val();

    if ((text.indexOf('.') != -1) && (text.substring(text.indexOf('.')).length > 2)) {
        event.preventDefault();
    }
});

演示

只需使用value.toFixed(2); 而已..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM