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

风流意气都作罢 提交于 2019-12-06 04:45:24

问题


I am getting weird behavior where javascript event that I am trying to dispatch is not being interpreted as event while when I inspect it is is Event.

Then it fails with following error

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

JSFiddle

I must be missing something obvious here.

$(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, "$1,") : num.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") + "." + 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="">

回答1:


The event in your script is not a DOM event but a jQuery event object and this cannot be used as parameter for EventTarget.dispatchEvent.

The original DOM event can be accessed through the property event.originalEvent




回答2:


I just had this problem, and it was solved by cloning the event:

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


来源:https://stackoverflow.com/questions/32371282/uncaught-invalidstateerror-failed-to-execute-dispatchevent-on-eventtarget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!