【问题标题】:onchange event in JavaScript - dependent option listJavaScript 中的 onchange 事件 - 依赖选项列表
【发布时间】:2019-04-26 06:17:30
【问题描述】:

我正在尝试使用 Excel 文件中的信息在 IE 中自动执行某些任务。我在 VBA 方面的知识让我可以用“是”回答网站上的问题,这应该会打开另一个问题,有更多选项可供选择。 如前所述,我得到了第一个是,但随后没有出现另一个问题。

这是来自网站的部分 JavaScript 代码和部分 HTML 代码:

    var elemData = jQuery.data( elem );

    // If no elemData is found then we must be trying to bind to one of the
    // banned noData elements
    if ( !elemData ) {
        return;
    }

    var events = elemData.events = elemData.events || {},
        eventHandle = elemData.handle, eventHandle;

    if ( !eventHandle ) {
        elemData.handle = eventHandle = function() {
            // Handle the second event of a trigger and when
            // an event is called after a page has unloaded
            return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
                jQuery.event.handle.apply( eventHandle.elem, arguments ) :
                undefined;
        };

<p>
	<span class="i_lbReq">First question</span> //this line has the 'change: eventHandle' event/function associated to it
    <br></br>
	<select name="meetingQuestionAnswer(220674)" class="auto-save">
    <option value=""> --- --- </option>
    <option value="Yes" selected="">Yes</option>
    <option value="No">No</option>
    <option value="Pending">Pending</option>
    </select>
 </p>
<div class="recursive-question" id="meetingDependentQuestion(220674)"> //this line has the 'change: eventHandle' event/function associated to it
<div id="meetingQuestionAnswerDiv(221010)">
    	<input name="meetingQuestionValidatorRule(221010)" type="hidden" value="">
      <input name="meetingQuestionReadOnly(221010)" type="hidden" value="">
      <p>
	<span class="i_lb">Dependent question 1</span>
        <br></br>
        <select name="meetingQuestionAnswer(221010)" class="auto-save">
        <option value="" selected=""> --- --- </option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
        </select>
      </p>
	<div class="recursive-question" id="meetingDependentQuestion(221010)">
        <div id="meetingQuestionAnswerDiv(221011)">
    	  <input name="meetingQuestionValidatorRule(221011)" type="hidden" value="">
          <input name="meetingQuestionReadOnly(221011)" type="hidden" value="">
          <p>
            <span class="i_lb">Dependent question 2</span>
            <br></br>
            <select name="meetingQuestionAnswer(221011)" class="auto-save">
            <option value="" selected=""> --- --- </option>
            <option value="Yes">Yes</option>
            <option value="No">No</option>
            </select>
          </p>
			<div class="recursive-question" id="meetingDependentQuestion(221011)">
		  </div>
		</div>
   </div>
  </div>
 </div>

我已尝试使用 VBA 在第一个问题元素上使用 .FireEvent "onchange" 方法,但它不起作用

Dim ieApp As SHDocVw.InternetExplorer
Set ieApp = IEWindowFromTitle("Profile Form Detail View")
If Not ieApp Is Nothing Then
    Set ieDoc = ieApp.document
End If


For Each cell In GMF.Sheets("BUDGET").Range("B4:B" & Cells(Rows.count, 2).End(xlUp).Row)
    If cell.Value = "MRR" Then
        ieDoc.getElementsByName("meetingQuestionAnswer(220674)")(0).Value = "Yes" 'this works fine
        ieDoc.getElementsByName("meetingQuestionAnswer(220674)")(0).FireEvent "onchange" 
        ieDoc.getElementsByName("meetingQuestionAnswerDiv(221010)")(0).Value = "Yes" 'this one never appears after saying yes in the previous question
    End If
Next cell

【问题讨论】:

  • @QHarr 我在问题中添加了它
  • 请问这是公共网址还是私人网址?并且您引用的元素是否应该在循环期间更改,因为即使不止一次的单元格包含 MRR,目前也只引用了 2 个元素?
  • 不幸的是是私有的.. 但如果需要我可以显示更多代码
  • 只是试图将您的第一个元素可视化为下拉菜单而不是输入元素。所以,我本来期望像 ieDoc.querySelector("select[name='meetingQuestionAnswer(220674)']'").click :ieDoc.querySelector("[name='meetingQuestionAnswer(220674)'] [value=Yes] ").Selected = True
  • 我在来自对象“JScriptTypeInfo”的方法“querySelector”中遇到错误

标签: javascript html vba internet-explorer dom-events


【解决方案1】:

尝试参考下面的代码示例可能有助于解决您的问题。

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

vSelect.dispatchEvent event_onChange 

参考:

Vba, HTMLSelect: FireEvent OnChange or DispatchEvent

【讨论】:

  • 谢谢!!!!工作完美!感谢您的参考。在我看来,我知道我需要这样做,但不知道该怎么做。
  • 我又遇到了一些问题,但我会编辑代码,因为我看到了一些可能有帮助的东西
  • 您需要通过详细的示例和步骤告知我们,例如您遇到问题的位置以及到目前为止您尝试了什么。它可以帮助我们更好地理解问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多