我正在开发一个扩展,使用来自其他网站的数据自动填充表单.我有一个具有不同选项的上下文菜单,每个选项都有不同的数据,但完成相同的表单.数据是动态的,取决于其他网站.使用"chrome.tabs.executeScript"我可以插入一些数据,但我无法触发事件"onchange"的字段.可以更改选择的值,但网站有一个事件"onchange",当我更改值时不会触发,而不是通过"chrome.tabs.executeScript"运行"onchange".它显示的错误是:"未捕获的TypeError:对象#的属性'onchange'不是函数#HTMLSelectElement"
编辑:
manifest.json的:
"content_scripts": [
{
"matches": ["url_where_the_code_must_run"],
"js": ["billing.class.js", "script.js"]
}
],
.
.
.
"permissions": [
"tabs",
"url_from_where_i_get_the_data",
"url_where_the_code_must_run"
],
Run Code Online (Sandbox Code Playgroud)
的script.js:
select = document.createElement('select');
var option = document.createElement('option');
option.value = "";
option.innerHTML = "Select Client";
select.appendChild(option);
select.onchange = function() {
var client = Billing.getClient(this.value);
if (client) {
document.getElementsByName("country")[0].value = client.country_id;
document.getElementsByName("country")[0].onchange();
}
}
for (var i = 0; i < Billing.clients.length; i++) {
option = document.createElement('option');
option.value = Billing.clients[i].id;
option.innerHTML = Billing.clients[i].name;
select.appendChild(option);
}
form.parentElement.insertBefore(select, form);
Run Code Online (Sandbox Code Playgroud)
执行此行时,显示"未捕获的TypeError:对象#的属性'onchange'不是函数#HTMLSelectElement":
document.getElementsByName("country")[0].onchange(); …Run Code Online (Sandbox Code Playgroud)