JavaScript依赖选项列表中的onchange事件 [英] onchange event in JavaScript - dependent option list
问题描述
我正在尝试使用Excel文件中的信息自动执行IE中的某些任务。我在VBA中的知识让我得到了一个点,我在网站上用是回答了一个问题,并且应该打开另一个问题,选择更多选项。
如上所述我得到了第一个是,但后来没有出现另一个问题。
I am trying to automate some task in IE with information from an Excel file. My knowledge in VBA lets me get to a point where I answer to a question in the website with "yes" and that should open another question with more options to select. As said I get to the first yes but then the other question doesn't appear.
以下是网站JavaScript代码的一部分HTML代码:
Here is part of the JavaScript code from the website and part of the HTML code:
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来使用方法。第一个问题元素上的.FireEventonchange但它不起作用
I have tried with VBA to use the method .FireEvent "onchange" on the first question element but it didn't work
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
推荐答案
尝试参考以下代码示例可能有助于解决您的问题。
Try to refer code example below may help to solve your issue.
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或DispatchEvent
这篇关于JavaScript依赖选项列表中的onchange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!