Uncaught InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is null

Uncaught InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is null

我的行为很奇怪,我尝试发送的 Javascript 事件没有被解释为事件,而当我检查它时却被解释为 Event

然后失败并出现以下错误:

Uncaught InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event provided is null.

JSFiddle

我肯定遗漏了一些明显的东西。

$(function () {
    $("[type='tel']").keydown(function (event) {
        var position = this.selectionStart;
        var $this = $(this);
        var val = $this.val();
        if (position == this.selectionEnd &&
            ((event.keyCode == 8 && val.charAt(position - 1) == "," && val.substr(0, position - 1).indexOf(".") == -1)
            || (event.keyCode == 46 && val.charAt(position) == "," && val.substr(0, position).indexOf(".") == -1))) {
            event.preventDefault();
            if (event.keyCode == 8) {
                $this.val(val.substr(0, position - 2) + val.substr(position));
                position = position - 2;
            } else {
                $this.val(val.substr(0, position) + val.substr(position + 2));
            }
            $this.trigger('keyup', { position: position });
        } else {
            if (event) {
                this.dispatchEvent(event);
            }
        }
    });

    $("[type='tel']").keyup(function (event, args) {
        if (event.which >= 37 && event.which <= 40) {
            event.preventDefault();
        }

        var position = args ? args.position : this.selectionStart;
        var $this = $(this);
        var val = $this.val();
        var parts = val.split(".");
        var valOverDecimalPart = parts[0];
        var commaCountBefore = valOverDecimalPart.match(/,/g) ? valOverDecimalPart.match(/,/g).length : 0;
        var num = valOverDecimalPart.replace(/[^0-9]/g, '');
        var result = parts.length == 1 ? num.replace(/(\d)(?=(\d{3})+(?!\d))/g, ",") : num.replace(/(\d)(?=(\d{3})+(?!\d))/g, ",") + "." + parts[1].replace(/[^0-9.]/g, "");
        $this.val(result);
        var commaCountAfter = $this.val().match(/,/g) ? $this.val().match(/,/g).length : 0;
        position = position + (commaCountAfter - commaCountBefore);
        this.setSelectionRange(position, position);
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input class="tooltip form-control input-validation-error" data-val="true" data-val-number="The field Sale price (£) must be a number." data-val-range="The sale price must be between £10,000 and £50,000,000" data-val-range-max="50000000" data-val-range-min="10000" data-val-regex="Sale price must be numeric characters only" data-val-regex-pattern="^\d+$" data-val-required="Please enter a sale price" id="SalePrice" name="SalePrice" placeholder="" tabindex="7" type="tel" value="">

您脚本中的 event 不是 DOM event but a jQuery event object and this cannot be used as parameter for EventTarget.dispatchEvent

原来的DOM事件可以通过属性event.originalEvent

访问

我刚遇到这个问题,通过克隆事件解决了:

if (event && event.originalEvent) {
    var oe = event.originalEvent;
    this.dispatchEvent(new oe.constructor(oe.type, oe));
}