我的应用程序是使用 React 创建的。通过使用 dispatchEvent 我试图使用 JS 预填充表单字段值。它在所有浏览器中都能正常工作,但在 IE 11 中不行。
IE 控制台显示对象不支持错误。
我用来预填表单字段的代码。
$(document.body).on('change', '#retailer', function() {
var retail_val = $(this).val();
//Storing the retailer value in the respective hidden text field.
if (retail_val != "0") {
var el = document.getElementById("text_6551650686371-InputField");
el.value = retail_val;
//calling the function
doEvent(el, 'input');
}
if (retail_val == "0") {
var el = document.getElementById("text_6551650686371-InputField");
el.value = "";
//calling the function
doEvent(el, 'input');
}
});
function doEvent(obj, event) {
event = new Event(event, {
target: obj,
bubbles: true
});
return obj ? obj.dispatchEvent(event) : false;
}
This is the error screenshot in IE 11 注意:我尝试使用 .val() 方法,因为我的应用程序是使用 React 创建的,它无法正常工作。请帮我在 IE 中预填表单字段值。此外,当我更改选项卡时,字段中的值会关闭。
最佳答案
根据 this answer ,如果您将事件名称更改为“textinput”(检查浏览器后执行此操作),它可能会起作用
关于javascript - 使用 dispatchEvent 预填充表单字段在 IE 中不起作用,在其余所有浏览器中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45428515/