【发布时间】:2018-05-26 09:39:05
【问题描述】:
我正在开发一个使用 chrome 插件来操作网站内容并添加更多功能的项目。
我有一个<select> 元素要操作:
<select id="thePage:form:pBlock:address:addressTypeSelectionDropdown" name="thePage:form:pBlock:address:addressTypeSelectionDropdown" size="1" onchange="onchangeOfPicklist();">
<option value="User Entry">User Entry</option>
<option value="CustomerID">CustomerID</option>
<option value="Closest to Customer">Closest to Customer</option>
<option value="Location">Location</option>
<option value="Customer's Address" selected="selected">Customer's Address</option>
</select>
我想在特定的 if 条件下更改选择选项,但这似乎不管我做什么都行不通。到目前为止,我几乎尝试了所有方法,但似乎 onchange 触发器正在发挥作用,否则单击模拟并尝试更改选项似乎不起作用。到目前为止,我尝试了不同的方法,但它们似乎都没有触发选项:
let customerAddress = labelAddress.parent().next().first().find("select");
customerAddress.children('[value="Closest to Customer"]').trigger("change");
还有:
customerAddress.children('[value="Closest to Customer"]').attr("selected", "selected");
customerAddress.children('[value="Closest to Customer"]').change();
和
customerAddress
.find('option:Closest to Customer')
.prop('selected',true)
.trigger('change');
甚至模拟按键:
let e = jQuery.Event("keydown");
e.which = 13; // # enter
customerAddress.focus();
customerAddress.trigger(e);
这里我不知道为什么没有触发事件。
我在添加事件监听,控制台没有提示事件触发:
document.addEventListener("keydown", function (event) {
console.log("KeyPress: " + event.which);
});
或者像这样:
let e = jQuery.Event("keydown");
e.which = 13; // # enter
customerAddress.focus();
customerAddress.attr("size", 5);
customerAddress.children('[value="Closest to Customer"]').focus();
customerAddress.children('[value="Closest to Customer"]').trigger(e);
请注意,我正在开发 chrome 插件,我只能更改此插件的源代码并操作网页上的元素,无法更改网页/脚本和函数的源代码。
我的问题是,有没有办法触发<select> 元素中嵌入的onchange 函数?
【问题讨论】:
-
在
let customerAddress = labelAddress.parent().next().first().find("select");中,labelAddress是什么? -
let labelAddress = $("label:contains('Address Type')");如果这是您想知道的,我会正确获取元素。我可以操纵 -
是的,我想知道。 :)
-
你的
label在哪里?你能尝试用所有涉及的元素对问题进行有效的 sn-p 吗?
标签: javascript typescript drop-down-menu