event.preventDefault()不适用于android chrome

问题描述:

event.preventDefault()无法在Chrome Android操作系统上运行。而同样的行动正在对Chrome IOS起作用。
我甚至使用了event.stopPropagation(),event.stopImmediatePropagation()。

event.preventDefault() not working on Chrome Android OS. Whereas the same action is working on chrome IOS. I even used event.stopPropagation(), event.stopImmediatePropagation().

HTML:

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

Java脚本:

function validatePaymentToTwoDecimal(el, evt) {

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


基于在类似问题的答案上,您应该可以执行此操作来过滤输入:

Based on an answer for a similar question, you should be able to do this to filter the input:

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

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



Vanilla JavaScript解决方案



Vanilla JavaScript solution

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);
});

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

This works by removing digits and decimal point characters using a regular expression.