简体   繁体   English

如何使用纯JavaScript激活级联下拉自动选择

[英]How to activate Cascading Dropdown auto-selection using pure javascript

I'm trying to select a dropdown item via an Execute Javascript macro inside Keyboard Maestro. 我试图通过Keyboard Maestro内部的Execute Javascript宏选择一个下拉项。 The code works to select and validate the dropdown item, but in the page I use this with, there are other dropdowns that should auto-populate based on the selection. 该代码可以选择并验证下拉菜单项,但是在与之配合使用的页面中,应根据选择自动填充其他下拉菜单项。 When I click a dropdown item manually, the other dropdowns update accordingly. 当我手动单击一个下拉菜单项时,其他下拉菜单也会相应更新。 Via the javascript I'm using, it selects the item but leaves the other dropdowns. 通过我使用的javascript,它选择了该项目,但保留了其他下拉菜单。 I need to use pure javascript and not jquery. 我需要使用纯JavaScript而不是jquery。 Does anyone have any ideas on how to get the page to react based on the javascript selection? 是否有人对基于javascript选择的页面响应有任何想法?

// variable to enter into dropdown
var vDropdownInput = (document.kmvar.vZEROInput);

// variable to get the Element by ID
var objSelect = document.getElementById("web-selection_"+document.kmvar.vZEROIndex+"_");

// function to do the work
setSelectedValue(objSelect, vDropdownInput);


// function definition
function setSelectedValue(object, value) {
    for (var i = 0; i < object.options.length; i++) {
        if (object.options[i].text === value) {
            object.options[i].selectedIndex = i;
            return;

    }
}
    // Throw exception if option `value` not found.
    var tag = object.nodeName;
    var str = value;
    return str;
}

edit: I found this related post but I just don't get the proposed solutions. 编辑:我找到了这个相关的职位,但我只是没有得到建议的解决方案。 Select item in CascadingDropDown via JavaScript & invoke update 通过JavaScript在CascadingDropDown中选择项目并调用更新

How do I programmatically force an onchange event on an input? 如何以编程方式在输入上强制发生onchange事件?

This answered my question. 这回答了我的问题。

var element = document.getElementById('just_an_example');
var event = new Event('change');
element.dispatchEvent(event);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM