gpt4 book ai didi

javascript - 未捕获的 InvalidStateError : Failed to execute 'dispatchEvent' on 'EventTarget' : The event provided is null

转载 作者:搜寻专家 更新时间:2023-11-01 05:30:10 24 4
gpt4 key购买 nike

我遇到奇怪的行为,我尝试分派(dispatch)的 Javascript 事件没有被解释为事件,而当我检查它时它是 Event

enter image description here

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

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, "$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="">

最佳答案

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

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

关于javascript - 未捕获的 InvalidStateError : Failed to execute 'dispatchEvent' on 'EventTarget' : The event provided is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32371282/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com