使用VBA执行javascript函数 [英] execute javascript function using VBA

查看:198
本文介绍了使用VBA执行javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用VBA执行javascript函数,但没有成功.

I am trying to execute a javascript function using VBA but without success.

我在列表框中选择一个项目.

I select an item in the list box.

我的目标:选定项目后刷新结果.

My goal: refresh the result after item selected.

VBA:

IEdoc_NEW2.getElementById("native_dropdown_selected_size_name").selectedIndex = 1
Call IEdoc_NEW2.parentWindow.execScript("dummySubmitButton()", "JavaScript")

HTML:

<select name="dropdown_selected_size_name" tabindex="-1" class="a-native-dropdown" id="native_dropdown_selected_size_name" autocomplete="off" data-a-touch-header="Taille">
  <option id="native_size_name_-1" value="-1" selected="" data-a-id="size_name_-1">Sélectionner</option>
  <option class="dropdownAvailable" id="native_size_name_0" value="0,B07DHJZSB3" data-a-id="size_name_0" data-a-html-content="S" data-a-css-class="dropdownAvailable">
    S </option>
  <option class="dropdownUnavailable" id="native_size_name_1" value="1,B07DHKZM7P" data-a-id="size_name_1" data-a-html-content="M" data-a-css-class="dropdownUnavailable">
    M
  </option>
  <option class="dropdownUnavailable" id="native_size_name_2" value="2,B07DHK697N" data-a-id="size_name_2" data-a-html-content="L" data-a-css-class="dropdownUnavailable">
    L
  </option>
</select>

JavaScript:

if (typeof(TwisterNonJs) == 'undefined') {
  window.TwisterNonJs = {};
  TwisterNonJs.handleDropDown = [];
}
TwisterNonJs.handleDropDown[1] = function() {
  var twisterUpdateButton = "submit.twisterUpdateButton_1"
  document.getElementById("dummySubmitButton").setAttribute("name", twisterUpdateButton);
  document.forms['twister'].submit();
};
document.getElementById("native_dropdown_selected_size_name").onchange = function() {
  TwisterNonJs.handleDropDown[1]();
};

但这不起作用.

推荐答案

尝试onchange fireEvent

Try onchange fireEvent

IEdoc_NEW2.getElementById("native_dropdown_selected_size_name").FireEvent "onchange"

您还可以尝试添加一个事件然后将其触发

You could also try adding an event then firing it

Dim evt As Object
Set evt = .document.createEvent("HTMLEvents")
evt.initEvent "change", True, False

IEdoc_NEW2.querySelector("#native_dropdown_selected_size_name").dispatchEvent evt

这篇关于使用VBA执行javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆