javascript - JavaScript 中的 onchange 事件 - 依赖选项列表
问题描述
我正在尝试使用 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
解决方案
尝试参考下面的代码示例可能有助于解决您的问题。
Dim event_onChange As Object
Set event_onChange = hdoc.createEvent("HTMLEvents")
event_onChange.initEvent "change", True, False
vSelect.dispatchEvent event_onChange
参考:
推荐阅读
- python-3.x - 在python中评估字符串中的数学表达式
- discord - lavalink 不和谐示例 cog 的问题
- reactjs - 使用返回 null 的条件渲染基于钩子状态渲染不同的组件
- reactjs - eslint sort-imports 混合单个和多个
- spring-integration - 正确的语法以在 ExpressionEvaluatingSqlParameterSourceFactory 中传递数据库序列下一个 val 参数
- laravel-5 - Laravel 5.4 - 如何计算表中员工的缺勤情况
- android - 从当前用户位置颤振中获取准确的地址
- c - Printf作为C编程中for循环中的参数
- linux-kernel - HiFive Unleashed 的引脚控制器的设备树中没有设备节点
- mongodb - mongodb:使用$all时,查询执行时间取决于输入参数的顺序