event.preventDefault() 不适用于 android chrome

Ven*_*ran 5 javascript

event.preventDefault() 不适用于 Chrome Android 操作系统。而同样的动作正在 chrome IOS 上工作。我什至使用了 event.stopPropagation()、event.stopImmediatePropagation()。

HTML:

 <input class="otherAmount" type="text"id="pVal" name="pVal" onkeydown="validatePaymentToTwoDecimal(this,event);"/>         
Run Code Online (Sandbox Code Playgroud)

Java脚本:

function validatePaymentToTwoDecimal(el, evt) {

        if(!$.isNumeric(evt.key)|| evt.key=="."){
            evt.preventDefault();
            evt.stopPropagation();
            evt.stopImmediatePropagation();
            return false;
        } else {
              return true;
        }
}
Run Code Online (Sandbox Code Playgroud)

Lar*_*sen 5

根据类似问题的答案,您应该能够执行以下操作来过滤输入:

jQuery 解决方案

$("#pVal").on(
    "input change paste",
    function filterNumericAndDecimal(event) {
        var formControl;

        formControl = $(event.target);
        formControl.val(formControl.val().replace(/[^0-9.]+/g, ""));
    });
Run Code Online (Sandbox Code Playgroud)

普通 JavaScript 解决方案

var pInput;

function filterNumericAndDecimal(event) {
    var formControl;

    formControl = event.target;
    formControl.value = formControl.value.replace(/[^0-9.]+/g, ""));
}

pInput = document.getElementById("pVal");
["input", "change", "paste"].forEach(function (eventName) {
    pInput.addEventListener(eventName, filterNumericAndDecimal);
});
Run Code Online (Sandbox Code Playgroud)

这是通过使用正则表达式删除数字和小数点字符来实现的。